1

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">
informatik01
  • 16,038
  • 10
  • 74
  • 104
user2515189
  • 539
  • 3
  • 9
  • 19
  • I did not get you.Please help me where I am doing mistake.Even though I used sessions why I am getting null after auto refreshing web page.Please help me.. – user2515189 Jun 25 '13 at 09:14
  • I trying to help :) ! What I meant by the first comment was this : in the form action, as you posted it in your question, your forgot a tiny little `"`. Start by adding it and editing it on the question so we know it has nothing to do with the problem. Otherwise, are you getting any exception ? If so, please post its name or stack. – Akheloes Jun 25 '13 at 09:37

2 Answers2

2

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.

Akheloes
  • 1,352
  • 3
  • 11
  • 28
  • YEs.I got it.Thank you so much..I used selopt instead of selectedUrl.It is working now.Thank you – user2515189 Jun 25 '13 at 11:29
  • Oh it's nothing ! Next time this happens to you just trust your logic and search for typos or soft-mistakes (ones were you mixed some variables or such). Good luck :) – Akheloes Jun 25 '13 at 11:33
  • But after getting refreshing of the page if I click on another button with in the frame and come back the page is not getting refreshed.Please help me.Why it is not getting refreshed once I come back from another frame. – user2515189 Jun 25 '13 at 11:39
  • Not sure but maybe because, from the other frame, you're not throwing a `GET` request to the servlet that does the refreshing. In other words, your doGet with the refreshing line is not being invoked. I am not very aware of the details of your use case, where is the frame you're talking about ? – Akheloes Jun 25 '13 at 11:42
2

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.

Community
  • 1
  • 1
informatik01
  • 16,038
  • 10
  • 74
  • 104
  • +1 for ever so superb an effort ! This deserves to be the accepted answer. – Akheloes Jun 25 '13 at 22:42
  • @Gloserio It is enough to be an addition to the accepted answer. Thank you for kind words ) – informatik01 Jun 25 '13 at 23:18
  • Ok. The notes of your answer are indeed very interesting, I wanted to mention the first one in my answer but I thought I should focus on giving a concise answer ; gladly, you did a better job. – Akheloes Jun 25 '13 at 23:26
  • @Gloserio Thanks. The OP just needs to compose his questions more clearly, especially the title, otherwise it does not reflect the core of his problem. But I'm sure he will get the idea. – informatik01 Jun 25 '13 at 23:33
  • 1
    I think he was confused, sort of. But yes, maybe some post-edit is most welcome. This can mislead into thinking the issue here is about `request` or `servlet` or `refresh` we're it's truly about much less. – Akheloes Jun 25 '13 at 23:41