1

servlet1

request.getSession().setAttribute("user", user.getUid());
request.getRequestDispatcher("home.jsp").forward(request,response);

home.Jsp code

<h5>Servlet communicated message to JSP: ${user}
     Please Select an image to upload</h5>   

<form action = "SaveInDBServlet" method = "GET"
         enctype = "multipart/form-data">
         <input type = "file" name = "filepath" size = "50" accept="image/*" />
         <br />
         <input type = "submit" value = "Submit" />

      </form>

user value is sent from servlet1 to JSP and now I need to transfer it from JSP to servlet 2. I need to access the value of the user in servlet 2 How to do this?

$user is an attribute which was sent by servlet 1$user prints the value on this web page Now for further processing, this variable has to be sent to servlet2

7
  • you have to pass it with either GET or POST request to servlet Commented Aug 29, 2018 at 6:57
  • Can you please provide a code snippet Commented Aug 29, 2018 at 6:58
  • you provide the code what you have tried to call the servlet from JSP Commented Aug 29, 2018 at 6:59
  • I have edited the code Commented Aug 29, 2018 at 7:02
  • @HeenaMittal add the code in servlet1 that add the user value Commented Aug 29, 2018 at 7:03

2 Answers 2

1

First of all let me start with explaining with the overloaded getSession() methods. The no-arg getSession() always returns a new session object which is similar to getSession(true), but in the case of getSession(false) it checks for an existing session, if present returns it or else returns null.

Once you have set an attribute to session, it will be available to a user until his session is available (not null) or not invalidated, so your data user will be available in your JSP and servlet2 also. But the method you are using to set attribute to the session is vulnerable, because getSession() can also return a new session in which your attribute won't be present.

So the safe way is to retrieve the session object to a reference, check whether it is null or not the get or set the attribute.

HttpSession session = request.getSession(false);
if(session != null) {
    //  set/get data whatever you need. Avoids NullPointerException
}

Second way is to use a hidden attribute in JSP, like this

<input type="hidden" name="user" value="<%=request.getAttribute("user") %>"/>

So you just need to set the attribute to request and get its value in jsp and set the value for hidden field. Now in the second servlet use the getParameter method (returns String) to get the value.

String user = request.getParameter("user");
Sign up to request clarification or add additional context in comments.

1 Comment

using scriptlets is not encouraged,use JSTL instead.
1

In your JSP do this

<form action = "SaveInDBServlet" method = "GET"
     enctype = "multipart/form-data">
     <input type = "file" name = "filepath" size = "50" accept="image/*" />
     <br />
     <input type = "hidden" name= "user" value="${user}"/>
     <input type = "submit" value = "Submit" />

</form>

In your servlet GET method

String username = request.getParameter("user");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.