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..
Related
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.
I used a jsp include another jsp, but I don't get any result, only html tag content.
HelloEmp.jsp: it had iterator value with stuts2 tag.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix = "s" uri = "/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Example of List</h2>
<s:iterator value="helloList">
<s:property /><br/>
</s:iterator>
</body>
</html>
HelloAction.java: it had a string array
import java.util.ArrayList;
import java.util.List;
public class HelloAction {
private List<String> helloList = new ArrayList<String>();
public String execute() throws Exception {
helloList.add("Jacky");
helloList.add("Natali");
return "success";
}
public List<String> getHelloList() {
return helloList;
}
public void setHelloList(List<String> helloList) {
this.helloList = helloList;
}
}
employees.jsp: it included HelloEmp.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix = "s" uri = "/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<title>Employees</title>
</head>
<body>
<p>An example of the include tag: </p>
<s:include value = "example/HelloEmp.jsp"/>
</body>
</html>
struts.xml:
<package name = "helloworld" extends = "struts-default">
<action name = "hello" class = "example.HelloAction" method = "execute">
<result name = "success">HelloEmp.jsp</result>
</action>
</package>
When I opened employees.jsp, only can see tag content in HelloEmp.jsp, I can't see the iterator value:
But if I directly open HelloEmp.jsp, I got the result:
Please help me how to fix it? thank you!
Instead of using value use page
<jsp:include page="Demo.jsp" /> might work.
Finally I use action tag instead,
<s:action name="action_name" executeResult="true"/>
that works for me!
Thank you for you guys.
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%>
<%
}
%>
So to be more specific what Im trying to solve. How can I show the first element in my Arraylist:"sortedDomainList" in my jsp?
EDIT : Model and ArrayList working tried in normal java application.
MODEL short version.
public ArrayList<String> sortedDomainList = new ArrayList<String>();
public ArrayList<String> getSortedDomainList() {
return sortedDomainList;
}
public void setSortedDomainList(ArrayList<String> sortedDomainList) {
this.sortedDomainList = sortedDomainList;
}
}
CONTROLLER/SERVLET
package com.comparebet.controller;
import com.comparebet.model.*;
#WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
public Controller() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
ArrayList<String> sortedDomainList = new BeanModel().getSortedDomainList();
request.setAttribute("d", sortedDomainList);
RequestDispatcher view = request.getRequestDispatcher("view.jsp");
view.forward(request, response);
}
}
VIEW/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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>CompareBet</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Comparebet" method="get" accept-charset="UTF-8"></form>
<h1> ${d.sortedDomainList[0]}</h1>
</body>
</html>
This is my first try on MVC so I feel really lost when it comes down to servlet.
<%# 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>CompareBet</title>
</head>
<body>
<%
ArrayList<String> sortedDomainList= request.getAttribute("d");
for(String domain:sortedDomainList){
%>
<h1> <%= domain %> </h1>
<% } %>
</body>
</html>
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>