user3640712
user3640712

Reputation: 17

JSP and send object

I have problém with send servlet to JSP

I have "web.xml".

    <servlet>
    <description></description>
    <display-name>optimalizace5</display-name>
    <servlet-name>optimalizace5</servlet-name>
    <servlet-class>
BucketServlet2
 </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>optimalizace5</servlet-name>
    <url-pattern>/optimalizace5</url-pattern>
  </servlet-mapping>

and BucketServlet2:

public class BucketServlet2 extends HttpServlet{

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp)
        throws ServletException, IOException {

     request.getSession().setAttribute("vysledek_list", "10101101");
     getServletContext().getRequestDispatcher("/optimalizace4.jsp").forward(request, resp); 
     super.doPost(request, resp);
    }
}

and JSP:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Optimalizační úlohy</title>
</head>
<body>
<FORM action="/Eliminace_kosiku/optimalizace5" method="post">

Data:

<%=request.getParameter("vysledek_list")%>

</form>
</body>
</html>

For send from JSP to servlet is working. But send from servlet to JSP not working. I dont know, what is wrong??

Upvotes: 0

Views: 148

Answers (2)

Naman Gala
Naman Gala

Reputation: 4692

You are putting value in session, not in request object.

request.getSession().setAttribute("vysledek_list", "10101101");
//          ^ setting attribute in session object.

So you need to take out value from session object in jsp.

Session is implicit object in jsp, so you can directly use it like

<%=session.getAttribute("vysledek_list")%>

Note: Don't forget to remove it from session if you don't need it further.

<%session.removeAttribute("vysledek_list");%>

Another approach: With reference to this.

You can just change below line in your code of doPost method

request.getSession().setAttribute("vysledek_list", "10101101");

with

request.setAttribute("vysledek_list", "10101101");

Update : Addition in second approach, in jsp use getAttribute method instead of getParameter method.

<%=request.getAttribute("vysledek_list")%>

Upvotes: 1

Masudul
Masudul

Reputation: 21961

getParameter take the URL parameter value. At Servlet you set the value at session, so, you should use session.getAttribute instead of getParameter

<%=request.getSesstion().getAttribute("vysledek_list")%>

Another point, As you use forward method at Servlet so request.setAttribute enough to pass value to JSP page. You should not store temporary value(i.e. same request) at session. So, set the value at request instead of session like below.

 request.setAttribute("vysledek_list", "10101101"); 

Upvotes: 0

Related Questions