简体   繁体   中英

setAttribute to checkbox with servlet

here is what I have:

JSP:

 <input type="checkbox" name="no_del_file" <% 
if ("True".equals(request.getParameter("no_del_file"))) {
    out.print("checked=\"checked\"");

} %>/>

<%String test = request.getParameter("no_del_file"); %>


 <p><%=test%></p>

Java:

 boolean cbState = request.getParameter( "no_del_file" ) != null;
            System.out.println("cbstate: "+cbState);
            if (cbState == true) {
                request.setAttribute("no_del_file", "checked");
                String checker=(String) request.getAttribute("no_del_file");
                System.out.println(checker);
            }

 RequestDispatcher dispatcher = request.getRequestDispatcher("/runButtonCommand.jsp");
            dispatcher.forward(request, response);

The problem is that the output is:

 cbstate: true
 checked

The test gives "on" as print after submitting the form

but the checkbox is not checked itself after the servlet returns the responds. The tick is removed for some reason.

Any ideas?

Try this

<% if(request.getParameter( "no_del_file" )=="checked"){%>
<input type="checkbox" id="no_del_file" name="no_del_file" value="no_del_file" checked>
<%}%>

I finally got it figured out... If someone is interested here it is

 <%String test = (String)request.getParameter("no_del_file"); %>
<%String checked = "";%>

<% 
if ("on".equals(test)) {
    checked="checked=\"on\"";

} %>
<input type="checkbox" name="no_del_file" <%=checked%>>

It returned on when checked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM