WEB-INF doesn't contain the directory classes with .class files - java

When I run on tomcat server my java web application
eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ProvaUseBean2\WEB-INF doesn't contain the directory classes that must contain all the BeanClasses that I use in my index.jsp
my project structure
my index.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<jsp:useBean id="classBean" scope="request" class="logic.bean.ClassBean"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
my ClassBean:
package logic.bean;
public class ClassBean {
private String myAttr;
public void setMyAttr(String s) {
myAttr = s;
}
public String getMyAttr(String s) {
return myAttr;
}
}
When I run on TomcatServer I receive this error:
org.apache.jasper.JasperException: /index.jsp (line: [3], column: [0]) The value for the useBean class attribute [logic.bean.ClassBean] is invalid.

Related

HomeControllers not rendering

I'm using SpringToolSuite with Java and I'm trying to render an index.jsp page onto my localhost:8080, but my Localhost is only going to the Whitelabel error page again & again. I guess it doesn't seem to be recognizing my Homecontroller.java file. The servers are running and have been refreshed. My files are correct. I see no issues anywhere else. What could be wrong that I'm not seeing? How can I render my page?
HomeController.java :
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping("/your_server")
public class HomeController {
#RequestMapping("")
public String index(HttpSession session) {
if (session.isNew()) {
session.setAttribute("counter", 0);
}
Integer count = (Integer) session.getAttribute("counter");
count += 1;
session.setAttribute("counter", count);
return "index.jsp";
}
#RequestMapping("/counter")
public String showCounter(HttpSession session, Model model) {
if (session.isNew()) {
session.setAttribute("counter", 0);
}
model.addAttribute("counter", session.getAttribute("counter"));
return "counter.jsp";
}
#RequestMapping("/reset")
public String resetCounter(HttpSession session) {
session.invalidate();
return "forward:/";
}
}
CounterApplication.java:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class CounterApplication {
public static void main(String[] args) {
SpringApplication.run(CounterApplication.class, args);
}
}
index.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome! Want to count some stuff?</title>
</head>
<body>
How many times has this page been visited?
<p>Oh, hi.</p>
</body>
</html>
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<c:out value="${2+2}"/>
counter.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>How many times are we going to go through this?!</title>
</head>
<body>
<c:out value="${ counter }" />
Go back.
Reset the counter.
</body>
</html>

How to pass the array list from servlet to JSP?

In the servlet:
List<myItem> yourObjectToReturn = search.parserContent();
request.setAttribute("yourObjectToReturn",yourObjectToReturn);
the array yourObjectToReturn consists 3 variable(id, txtfile, sentence) which you can see from
the myItem class
public class myItem{
String sentence;
int id;
String txtfile;
// public myItem(){
// }
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getTxtfile(){
return txtfile;
}
public void setTxtfile(String txtfile){
this.txtfile = txtfile;
}
public String getSentence(){
return sentence;
}
public void setSentence(String sentence){
this.sentence = sentence;
}
}
how to display the id, txtfile, sentence in the JSP separately? How to pass the arraylist from servlet to JSP .
the JSP : how to edit my JSP. I got error of my JSP:
type safety: unchecked cast from objectto arraylist
<%# page import="java.io.*" %>
<%# page import="java.net.*" %>
<%# page import="java.util.*" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% List<myItem> myList = (ArrayList<myItem>) request.getAttribute("yourObjectToReturn"); %>
The search Result SENTENCE IS: <%=myList %> --%>
</body>
</html>
Don't use scriptlets in your jsp page.
Include the JSTL standard taglib via:
<%# taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
Then in your JSP use the iteration tag:
<c:forEach items="${requestScope.yourObjectToReturn}" var="current">
<c:if test="${current.sentence== 'secret' }">
<h1>seeeeeeeeeecret revealed</h1>
</c:if>
</c:forEach>
Where:
${requestScope.yourObjectToReturn} is your collection object.
And (during each iteration):
${current} is your actual element.
For further reference look http://docs.oracle.com/javaee/5/tutorial/doc/bnahq.html
And to avoid weird errors: don't forget to import myItem class (Should really be MyItem, thou...)
EDIT: Before digging deep into JSTL, I suggest you to take a reading of this other question. Particularly focus on the selected answer, it provides great insights.
To retrieve the Id and Txtfile from the List you need to iterate with using for example a for loop like:
...
for (int i=0; i < myList.size(); i++) {
%>
<%=myList.get(i).getId()%>
<%=myList.get(i).getTxtfile())%>
<%}%>
You can use for loop and html together as follows:
<%
#SupressWarnings("unchecked")
List<mtItem> myList
for (MyItem myitem : myList)
{
%>
The search result is <%=myitem%>
<%
}
%>

Input fields not being validated while using Java annotations

I'm having issues understanding how to validate fields in JSF by using annotations. I've read every tutorial I can get my hands on, but nothing. I'm using JSF 2.2 with Tomcat 8.
I'm simply trying to make sure an email fields is at least 5 characters long. That's it. Here's my class and JSP page.
How can I get my message to be displayed and the data validated?
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%# taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html>
<f:view>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:p="http://java.sun.com/jsf/passthrough">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>HTML5 Validation</title>
</head>
<body>
<h:form>
<h:inputText value="#{user.email}" id="email">
</h:inputText>
<h:commandButton action="#{user.runIt}" value="Run it" />
</h:form>
<h:messages/>
</body>
</html>
</f:view>
User.java
package com.jacroe.html5Validation.model;
import javax.validation.constraints.Size;
public class User {
private String email;
public String runIt() {
return "ran";
}
public String getEmail() {
return email;
}
public void setEmail(#Size(min=2, message="Size too short") String email) {
this.email = email;
}
}

Content-Type not allowed: fileUpload in Struts 2

I am new to Struts 2 and trying to do use fileUpload interceptor. I am attaching all my code layers
Action Class (FileUploadAction):
package com.caveofprogramming.actions;
import java.io.File;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
public String getFileUploadContentType() {
return fileUploadContentType;
}
public void setFileUploadContentType(String fileUploadContentType) {
this.fileUploadContentType = fileUploadContentType;
}
public String getFileUploadFileName() {
return fileUploadFileName;
}
public void setFileUploadFileName(String fileUploadFileName) {
this.fileUploadFileName = fileUploadFileName;
}
public File getFileUpload() {
return fileUpload;
}
public void setFileUpload(File fileUpload) {
this.fileUpload = fileUpload;
}
#Action( value = "/fileUpload",
results={#Result(name="success",location="/success.jsp"),
#Result(name="error",location="/error.jsp"),
#Result(name="input",location="/error.jsp")
},
interceptorRefs={
#InterceptorRef(
params={"allowedTypes","image/jpeg,image/jpg,application/zip",
"maximumSize","1024000"},
value="fileUpload"
),
#InterceptorRef("defaultStack"),
#InterceptorRef("validation")
}
)
public String execute(){
try{
return SUCCESS;
} catch(Exception e){
return ERROR;
}
}
public String display() {
return NONE;
}
}
error.jsp:
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:fielderror/>
</body>
</html>
Success.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Success
</body>
</html>
fileUpload.jsp:
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head />
</head>
<body>
<h1>Struts 2 <s:file> file upload example</h1>
<s:form method="post" enctype="multipart/form-data" action="fileUpload">
<s:file label="File One" name="fileUpload" />
<s:submit />
</s:form>
</body>
</html>
I am not understanding why I am getting this error
"Content-Type not allowed: fileUpload "photography-104a.jpg" "upload_37fbf440_169b_4687_af65_93c8c967256c_00000000.tmp" image/pjpeg"
Although my uploading file format is .jpg.
You are getting this error probably because you don't allow files with content type image/pjpeg. Use parameter of fileUpload interceptor to define allowed MIME types
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/pjpeg</param>
</interceptor-ref>

Exception while using JavaBean in JSP

I can't solve this problem can you help me please.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="musteri" class="beanler.MusteriBean" scope="request" ></jsp:useBean>
<%musteri.setIsim("Ferid");%>
<%=musteri.getIsim() %>
</body>
</html>
EXCEPTION: SEVERE: Servlet.service() for servlet [jsp] in context with
path [/Servlet_Projesi] threw exception [/beanTest.jsp (line: 11,
column: 0) The value for the useBean class attribute
beanler.MusteriBean is invalid.] with root cause
org.apache.jasper.JasperException: /beanTest.jsp (line: 11, column: 0)
The value for the useBean class attribute beanler.MusteriBean is
invalid.
package beanler;
public class MusteriBean {
private String isim;
private String soyad;
public String getIsim() {
return isim;
}
public void setIsim(String isim) {
this.isim = isim;
}
public String getSoyad() {
return soyad;
}
public void setSoyad(String soyad) {
this.soyad = soyad;
}
}
try to set the property by using <jsp:setProperty>
<jsp:useBean id="musteri" class="beanler.MusteriBean" scope="request" >
<jsp:setProperty name="musteri" property="isim" value=" Ferid" />
</jsp:useBean>
while displaying you can use <%=musteri.getIsim() %>
Two points you can give a try.
[1]Initialise your class properties like
private String isim = null;
private String soyad = null;
[2][Not mandatory]implement Serializable like
public class MusteriBean implements java.io.Serializable
I also found another solution.
<jsp:useBean id="musteri" class="beanler.MusteriBean" scope="request" ></jsp:useBean>
I changed it to:
<jsp:useBean id="musteri" class="beanler.MusteriBean" scope="request" />
and that works..

Categories

Resources