Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7627587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:28:45+00:00 2026-05-31T05:28:45+00:00

Following is the servlet class that sets the name by invoking a method on

  • 0

Following is the servlet class that sets the name by invoking a method on the object of a bean class and then forwards to a jsp page .

package BeanTesters;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

 public class Controller extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
    Bean bean = new Bean(); 
    bean.setName("Suhail Gupta");
    //request.setAttribute("name", bean);
    RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
    rd.forward(request, response);
  }
}

And this is the bean class :

package BeanTesters;

 public class Bean {

  private String name = null;

  public void setName(String n) {
    name = n;
  }

  public String getName() {
    return name;        
  }
 }

following is a jsp snippet that tries to display the name set by the servlet:

<jsp:useBean id="namebean" class="BeanTesters.Bean" scope="request" />
    Person created by the Servlet : <jsp:getProperty name="namebean" property="name" />

The result i get is : Person created by the Servlet : null Why do i get a null value ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:28:46+00:00Added an answer on May 31, 2026 at 5:28 am

    Because the jsp:useBean tag tries to get a bean in the attribute "namebean" of the request, and since you didn’t store anything under this attribute name, it creates one. The bean instance used by the JSP is thus a different instance than the one created in the servlet.

    Put the following code in your servlet, and you’ll get the desired behavior:

    request.setAttribute("namebean", bean);
    

    Note that the jsp:xxx tags are completely obsolete, and should not be used anymore. You should instead use the JSP expression language (EL), and the JSTL:

    Person created by the Servlet : ${namebean.name}
    

    Or even better, to make sure the potential HTML characters present in the name are properly escaped:

    Person created by the Servlet : <c:out value="${namebean.name}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the following method to send out an object from a servlet :
I have servlet that does the following: public class LoginServlet extends HttpServlet { protected
in my servlet i called an instance of a class.java( a class that construct
I have the following class that I'm trying to make persistent, and am getting
I have the following class that doesn't seem to be returning a List from
Following up from this question: How can I unlock a file that is locked
Sorry about the vague title but I have a servlet with the following mapping
I am running a JSP/Servlet Web application and out of nowhere we experienced some
To enable Servlet, JSP, JSF etc to use a remote EJB , performing resource
I'm wondering is possible to integrate custom servlet logic with .jsp template view. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.