• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getter/Setter methods in Servlets

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.....
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic