1

The username of the person who logs in is present on the index page and the code works fine. But trying to access the variable username from the servlet is proving hard. Any ideas?

index.jsp

   <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
    <title><%=request.getParameter("username")%> - Cloud</title>  
      <link rel="stylesheet" href="css/bootstrap.css">
    </head>  
    <header class="page-header">

    <% String username = request.getParameter("username");%> // How can i get this variable?
    <h1 align="center" class="inline"><%=username%> - Files</h1>

    </header>

<body>  

Servlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

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

?

  • 1
    The doPost method is there to process POST request submitted through a form in your jsp. You should at least have that. – Angelo Oparah May 01 '16 at 21:31
  • I do. The problem is that a different servlet gets the username and puts it on the landing page ("welcome *user*").. But then when another servlet deals with a different request on the landing page, it cannot retrieve the username. Any ideas? – Jonathan Laliberte May 01 '16 at 21:33
  • 1
    Sorry that's not clear to me. All I can say from what you have posted is that the index.jsp and Servlet.java are not "communicating": that jsp is not sending any request to your Servlet. You could try and store the username in the session object and then retrieve it by session.getAttribute(), session being the session object you need to declare...but that might not be the best solution depending on your scenario. – Angelo Oparah May 01 '16 at 21:43
  • 1
    @AngeloOparah thanks ! Your mention of the session thing helped me find a workaround that works great. Cheers! – Jonathan Laliberte May 02 '16 at 00:20

1 Answers1

1

You have to call the servlet through the jsp page

How to call servlet through a JSP page

This is an official example from oracle of how to do that:

Jsp2Servlet.jsp

<HTML>
<HEAD> <TITLE> JSP Calling Servlet Demo </TITLE> </HEAD>
<BODY>
<!-- Forward processing to a servlet -->
<% request.setAttribute("empid", "1234"); %>
<jsp:include page="/servlet/MyServlet?user=Smith" flush="true"/>
</BODY>
</HTML>

MyServlet.java

public class MyServlet extends HttpServlet {

    public void doGet (HttpServletRequest request,
                   HttpServletResponse response) 
      throws IOException, ServletException {
      PrintWriter out= response.getWriter(); 
      out.println("<B><BR>User:" + request.getParameter("user"));
      out.println(", Employee number:" + request.getAttribute("empid") + "</B>");
      this.getServletContext().getRequestDispatcher("/jsp/welcome.jsp").
      include(request, response);
    }
}

welcome.jsp

<HTML>
<HEAD> <TITLE> The Welcome JSP  </TITLE> </HEAD>
<BODY>

<H3> Welcome! </H3>
<P><B> Today is <%= new java.util.Date() %>.  Have a nice day! </B></P>
</BODY>
</HTML>

The key is in the line:

<jsp:include page="/servlet/MyServlet?user=Smith" flush="true"/>

In this example a jsp call a servlet a that servlet call another jsp. The example is from this page: https://docs.oracle.com/cd/A87860_01/doc/java.817/a83726/basics4.htm

Community
  • 1
  • 1
Troncador
  • 3,356
  • 3
  • 23
  • 40