I am working on web application, where I used a class Calculator with following information:-
public class Calculator {
private static String name,familyname;
public Calculator() {
name = "Roberto";
familyname = "Sanchez";
}
public static String getName() {
return name;
}
public static String getFamilyname() {
return familyname;
}}
In my index.jsp file, I used following code:-
<%--
Document : login
Created on : Nov 3, 2016, 6:21:46 AM
Author : yati
--%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Information</title>
</head>
<body bgcolor="#00FFFF">
<%=new com.algeb.Calculator()%> <br>
First Name: <%= com.algeb.Calculator.getName()%> <br>
Family Name: <%= com.algeb.Calculator.getFamilyname()%> <br>
Check Result
</body>
</html>
Now, my webpage looks like this:-
My problem is that I don't to display the first line (Calling class with the help of object) i.e. com.algeb.Calculator#3490ac94. This is also important step as without this, I cannot initialize the first name and last name. Please help?
Try to use JSP beans:
<jsp:useBean id= "instanceName" scope= "page | request | session | application"
class= "packageName.className"/>
And than use id to call getName() and getFamilyname() methods, for example:
${instanceName.getFamilyname()}
${instanceName.getFamilyname()}
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 want to pass a data from controller method to jsp page. In doing so, using HttpServletRequest.setAttribute().
Now, I can pass it to the just next jsp page. But, I want to hold that data for few more pages.
In this case, what should I do?
Flow of Data:
Controller method1 --> jsp page1 --> jsp page2 --> jsp page3 --> jsp page4 --> Controller method2
I tried setting attribute in each page but it returns null value, as follows
<% request.setAttribute("accId", request.getAttribute("accountId")); %>
You have to use session in jsp when sending data from one page to another.
A demo to show this.
For example :
Create a DemoController class.
#Controller
public class DemoController {
#RequestMapping(value = "/getid", method = RequestMethod.POST)
public String getAccountID(Model model) {
model.addAttribute("accountId", "ABC1234"); // example
return "account";
}
}
Suppose, create an account.jsp.
<!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">
</head>
<body>
<%
String accountId = request.getAttribute("accountId");
out.println("account.jsp -> " + accountId);
session.setAttribute("accId", accountId);
%>
<form action="account2.jsp" method="post">
<input type="submit" name="Submit">
</form>
</body>
</html>
Create another page with the name account2.jsp :
<!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">
</head>
<body>
<%
String accId = (String) session.getAttribute("accId");
out.println("account2.jsp -> " + accountId);
// now you want to sent it to the another controller
// set the parameter in the session and retrieve it in the controller.
session.setAttribute("accountId", accId);
%>
</body>
</html>
Create a DemoController2 class :
#Controller
public class DemoController2 {
#RequestMapping(value = "/getid2", method = RequestMethod.POST)
public String getAccountId2(HttpSession session) {
String id = (String) session.getAttribute("accountId"); // example
System.out.println(id);
return "some-page-name";
}
}
I have written a hello world program in jsp and now i am trying to process forms via JSP.
My jsp form(GetName.jsp) looks like this
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<FORM METHOD=POST ACTION="SaveName.jsp">
Name <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
Email <INPUT TYPE=TEXT NAME=email SIZE=20><BR>
Age <INPUT TYPE=TEXT NAME=age SIZE=4>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
</body>
</html>
Similarly SaveName.jsp looks like this
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:useBean id="userData" class="javabeans.UserData" scope="session"/>
<jsp:setProperty name="userData" property="*"/>
</BODY>
</HTML>
</body>
</html>
And in the same project in a package named javabeans the class named UserData looks like this.
package javabeans;
public class UserData {
String username;
String email;
int age;
public void setUsername( String value )
{
username = value;
}
public void setEmail( String value )
{
email = value;
}
public void setAge( int value )
{
age = value;
}
public String getUsername() { return username; }
public String getEmail() { return email; }
public int getAge() { return age; }
}
Now when run GetName.jsp i get the following errors
D:\javaworkspace\Netbeans7-2\HelloWeb\build\generated\src\org\apache\jsp\SaveName_jsp.java:56: cannot find symbol
symbol : class UserData
location: class org.apache.jsp.SaveName_jsp
UserData user = null;
^
D:\javaworkspace\Netbeans7-2\HelloWeb\build\generated\src\org\apache\jsp\SaveName_jsp.java:58: cannot find symbol
symbol : class UserData
location: class org.apache.jsp.SaveName_jsp
user = (UserData) _jspx_page_context.getAttribute("user", PageContext.SESSION_SCOPE);
D:\javaworkspace\Netbeans7-2\HelloWeb\build\generated\src\org\apache\jsp\SaveName_jsp.java:60: cannot find symbol
symbol : class UserData
location: class org.apache.jsp.SaveName_jsp
user = new UserData();
3 errors
D:\javaworkspace\Netbeans7-2\HelloWeb\nbproject\build-impl.xml:930: The following error occurred while executing this line:
D:\javaworkspace\Netbeans7-2\HelloWeb\nbproject\build-impl.xml:284: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 2 seconds)
You need to import UserData class inside SaveName.jsp
Add this to the top of your jsp code in SaveName.jsp
<%# page import="javabeans.UserData" %>
I'm not sure what you want to implement over here but i can give you simple idea about jsp servlets
first you can create a simple jsp program. it will ask the user for name and email id and on form action redirect it to abc 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> My first JSP </title>
</head>
<body>
<form action="abc">
Please enter a name <br>
<input type="text" name="name"size="20px">
Please enter an email <br>
<input type="text" name="email"size="20px">
<input type="submit" value="submit">
</form>
</body>
</html>
and then create the "abc" servlet
place this code in your servlet. it will get the value from jsp page and display it.
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class abc extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// reading the user input
String name= request.getParameter("name");
String email= request.getParameter("email");
String msg="I'm"+name+"id is"+email;
PrintWriter out = response.getWriter();
out.println (
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">\n" +
"<html> \n" +
"<head> \n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html;
charset=ISO-8859-1\"> \n" +
"<title> Hi </title> \n" +
"</head> \n" +
"<body> \n" +
msg +
"</font> \n" +
"</body> \n" +
"</html>"
);
}
}
Define your servlet in "web.xml" . you need to do servlet mapping in web.xml file.
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>abc</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/abc</url-pattern>
</servlet-mapping>
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..
How do I POST an array of custom objects to a Struts 2 action in Java?
For example if I have the following Java object:
public class Person {
private String name;
private String lastName;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
And the following Action:
public class SavePersons extends ActionSupport {
private List<Person> persons;
#Override
public String execute() throws Exception {
// Do something
return SUCCESS;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
}
I'd like to do something like this in an HTML form:
<html>
<body>
<form method="POST" action="http://postHere">
<input type="text" name="persons[0].name" value="Name1"/>
<input type="text" name="persons[0].lastName" value="LastName1"/>
<input type="text" name="persons[1].name" value="Name2"/>
<input type="text" name="persons[1].lastName" value="LastName2"/>
<input type="submit" />
</form>
</body>
</html>
Any tips?
What you have looks good. It does not make a difference to struts2 if you post or get as far as setting values.
Using the same SavePersons class, except that I added a public List<Person> getPersons() method. This is required to make the solution work.
And using essentially the same form, except I prefer to write my forms using s2 tags where it makes sense (what puts some people off the form tags is the default s2 theme, you can globally set the theme to simple, the label attribute will not work but the UI tags will work just like you'd expect similar html elements to behave):
<%#taglib prefix="s" uri="/struts-tags"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Person Form</title>
</head>
<body>
<h1>Person Form</h1>
<s:form action="person-test" method="post">
<s:textfield name="persons[0].name" label="fName 1"/>
<s:textfield name="persons[0].lastName" label="lName 1"/>
<s:textfield name="persons[1].name" label="fName 2"/>
<s:textfield name="persons[1].lastName" label="lName 2"/>
<s:submit/>
</s:form>
</body>
</html>
Note that method="post" is not needed, it is the default.
Here is the page used to display the form data.
<%#taglib prefix="s" uri="/struts-tags"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>List of People</h1>
<s:iterator value="persons">
<s:property value="name"/> <s:property value="lastName"/><br/>
</s:iterator>
</body>
</html>
And it worked just fine.