-1

When the user fills the Sign up form(on page Reg.jsp) the data is successfully forwarded to the servlet(Regi.java) and when i use <%=request.getParameter("username")%> on one of my jsp pages(Imgu.jsp-which asks user to upload image) it works fine but when the same data is requested on another servlet(imgup.java) the value of parameter username is null.

I cannot understand why this happens i have tried creating a session but it did not work.

  • i hope you are clear on the concepts of request being `forward or Redirect` – Ankur Singhal Aug 23 '14 at 09:40
  • how are redirecting from Regi.java to Imgu.jsp, forward/include/redirect ? –  Aug 23 '14 at 09:47
  • because `username` is not in the request scope anymore – Ker p pag Aug 23 '14 at 09:47
  • in case u r using sendredirect() this won't work. Since the response is sent only when you submit to other page or use request dispatcher, both of which send the response object along with. Still if u want to achieve this, you may use request.setAttribute("",""); to set the value and request.getAttribute(""); to fetch the value.. – user63762453 Aug 23 '14 at 09:58
  • Please give precise interactions between the servlets and JSP. As currently written it is *unclear*. – Serge Ballesta Aug 23 '14 at 10:16

3 Answers3

1

Try this:

Regi.java

  HttpSession session = request.getSession();
  session.setAttribute("username", request.getParameter("username"));

Imgu.jsp

<form>
    ...
    <input type="hidden" name="username" value="${username}"/>
</form>

OR

Regi.java

  HttpSession session = request.getSession();
  session.setAttribute("username", request.getParameter("username"));

imgup.java

  String username = (String)request.getSession().getAttribute("username");
0

As the documentatioin says request.getParameter()

Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

So it is related to one request. Unless you provide parameter in query string or posted form in the request the other servlet it wont be there.

For data that should be shared a cross requests use the session scope as Arvind suggested.

Gas
  • 17,601
  • 4
  • 46
  • 93
0

I suspect the problem you are having has to do with the fact that you are dealing with multi-part requests (because you are uploading an image). Tomcat 7 has a feature that deals with this:

How to use HttpServletRequest#getParts() in a servlet filter running on Tomcat?

Otherwise, you may want to use a multipart library to extract the parameters.

Community
  • 1
  • 1
Zeki
  • 5,107
  • 1
  • 20
  • 27