Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Getter/Setter methods in Servlets

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Marshal
Posts: 67446
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic