alessandro
alessandro

Reputation: 1721

JSP Parameter Passing Issue

My page flow is like,

Jsp1 -> Jsp2 -> ... -> Servlet -> JspN -> ... 

where, Jsp1 is log in page where user has to give password and username. Now, I want to use username and password value in servlet page. Is it possible to get those value in servlet without passing parameters from Jsp1 to Jsp2 .... to Servlet?

Upvotes: 0

Views: 235

Answers (2)

seeker
seeker

Reputation: 6991

Yup that's a good approach, ideally though I dont see any reason why you cant use a servlet between jsp 1 and jsp 2.? That way you can retain the values in the request and then use them in your second jsp.

Upvotes: 0

adarshr
adarshr

Reputation: 62573

You can set them in session in Jsp2 stage. Get the parameters from request and set them in session as so:

<%
    session.setAttribute("username", request.getParameter("username"));
    session.setAttribute("password", request.getParameter("password"));
%>

To be able to do this, your Jsp1 should have fields defined with the same names as the request parameters.

<input type="text" name="username"/>
<input type="password" name="password"/>

Then in the Servlet, you can simply read them as so:

public void doGet(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession();

    String username = session.getAttribute("username");
    String password = session.getAttribute("password");
}

Upvotes: 1

Related Questions