Question

I have a JSP with drop down box. I select one option and submit that one. So now I get the information stored in the database of that selected option. Now I refresh the JSP (HTML page) automatically by setting in servlet as

//servlet code inside doGet() method
HttpSession session=request.getSession();
String selUrl=request.getParameter("urlsel");    
String opt=session.setAttribute("selectedUrl",selUrl);
String selopt=session.getAttribute("selectedUrl");
response.setHeader("Refresh","10;url="/SiteAvailabilityServlet?ursel="+selectedUrl);
//and forwarding request to result.jsp using RequestDispatcher..

//input.jsp code

<select name="urlsel">
<option value="abc">abc</option>
<option value="def">def</option>
</select>

For the first time when I select the option say abc and submitted manually it is giving me the correct result and showing the details from data base. After 10 seconds it is refreshed automatically with the same option abc (I do not want to change the option), but not displaying values. It is taking the

  request.getParameter("urlsel")    as null  after refreshing automatically.

Please help me. In result.jsp I am using

 <form method="get" action="/SiteAvaialabilityServlet">
Was it helpful?

Solution

I think your code should function if you replace this line :

response.setHeader("Refresh","10;url="/SiteAvailabilityServlet?ursel="+selectedUrl);

With this :

response.setHeader("Refresh","10;url=/SiteAvailabilityServlet?urlsel="+ selopt);

What changed is this :

  • An " was taken away from the URL in the header, I don't see why it should be there ;
  • It's urlsel not ursel ;
  • Why selectedUrl ? It's the name of the session attribute, what you want is the value of the session attribute. Since your have that (selopt), you might just want to use it.

Your HTML may want to be better taken care of, as far as this line is concerned, an " is missing :

<form method="get" action="/SiteAvaialabilityServlet> 

So replace it with this :

<form method="get" action="/SiteAvaialabilityServlet">

Hope all those typos are not in your original code.

P.S : Please do correct the code in your question to help everybody help you :).

Best of luck.

OTHER TIPS

As an addition to the accepted answer, here is a JSP (analogous to your previous question) to showcase how to preserve the value selected by using HTML select element after refreshing the page.

NOTICE: The usage of scriptlets is discouraged. In the following example scriptlets are used for the sake of simplicity, to simulate what the OP was trying to do in his example.

test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
    String selectValue = request.getParameter("course");
    if (selectValue != null) {
        session.setAttribute("savedCourse", request.getParameter("course"));
    }
%>
<!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="refresh" content="3; url=${pageContext.request.requestURL}?savedCourse=${sessionScope.savedCourse}">
</head>
<body>
    <h1>Test Page</h1>
    <form action="" method="post">
        <p>Choose some course</p>
        <select name="course">
            <option value="English" name="eng" <c:if test="${sessionScope.savedCourse == 'English'}">selected="selected"</c:if>>English</option>
            <option value="Math" name="mat" <c:if test="${sessionScope.savedCourse == 'Math'}">selected="selected"</c:if>>Math</option>
            <option value="Computer Science" name="sci" <c:if test="${sessionScope.savedCourse == 'Computer Science'}">selected="selected"</c:if>>Computer Science</option>
        </select>
        <p><input type="submit" value="Pass data" /></p>
    </form>
    <hr />
    <h2>Testing submitted and saved parameter</h2>
    <p>Passed "course" parameter (will display nothing after auto REFRESH) = <span style="color: #FF0000">${param.course}</span></p>
    <p>Stored "savedCourse" attribute = <span style="color: #FF0000">${sessionScope.savedCourse}</span></p>
</body>
</html>

For the additional info check out this answer: https://stackoverflow.com/a/17280533/814702


Some notes regarding your code:

  • What's the point in setting session attribute and in the next line getting this attribute from session, when it is already available anyways (in selurl variable)?

  • HttpSession#setAttribute(String name, Object value) method does not return anything (void), so you can't assign void to the variable opt of type String, like you do in your code. This code will not compile and give you an error: incompatible types.

  • The reason why request.getParameter("urlsel") returns null is because it was a refresh, thus a NEW request, and the request attributes are reset between new requests. Read here: Servlet Redirection to same page with error message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top