Preserving the selected value in an HTML select element after refreshing a JSP page

大憨熊 提交于 2019-12-02 06:15:54

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.

informatik01

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!