File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Getter/Setter methods in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Getter/Setter methods in Servlets" Watch "Getter/Setter methods in Servlets" New topic
Author

Getter/Setter methods in Servlets

Rajnarayan Deenadayalan
Greenhorn

Joined: Dec 16, 2003
Posts: 14

I am working on a employee form,creating a form bean for employee (setter and getter methods)
In doPost method of a servlet to set the values.but when trying to access getter methods in another JSP
Iam not able to ,it is returning null value.please suggest what would be the issue

Below is the code:

Bean class:

public class employee {
int empid;
String role;

public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}


Setting value in Servlet:

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

employee a=new employee();
a.setEmpid(100);
a.setRole("Installer");
getServletConfig().getServletContext().
getRequestDispatcher("/index.jsp").
forward(request, response);
}


getting Valuein another JSP:

index.jsp
<%
employee emps=new employee();
%>

<%emps.setEmpid(100); %>


the value returns null value
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
yang xiao
Greenhorn

Joined: Sep 21, 2009
Posts: 2

Servlet
request.setAttribute("a", a);

JSP:

${requestScope.a}

Expression Language:very strong Tool for passed the value from different Scope at JSP

e......my english is so so.....
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

getting Valuein another JSP:

index.jsp
<%
employee emps=new employee();
%>

<%emps.setEmpid(100); %>


the value returns null value


yes its obvious. you have set the value in Servlet and did not store/ pass ( in any means) to JSP , but still trying to access in JSP ?

Check out JSP attributes & scopes to find out which scope suits your need.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getter/Setter methods in Servlets
 
Similar Threads
DynaValidatorActionForm not working
Standard actions - doubt in HFS - page 414
Doubt in Standard Actions
Passing multiple listbox to bean using JSP
useBean doubt