单选按钮在jsp / servlet中提交表单后选择

时间:2013-04-11 10:03:34

标签: javascript jsp servlets

我正在用单选按钮提交表格。我希望在提交表格后检查该单选按钮。基本上我正在页面上做一些搜索过程。请检查代码。

在我的jsp上。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <% 
                String buttonvalue = (String) request.getParameter("radio");
        %>
        <%= buttonvalue %>

        <form method="post" action="SearchServlet">

            <script type='text/javascript'>
            var button = "<%= buttonvalue %>";
            if(button == "one")
            {
                alert("1");
                document.getElementById("rdone").checked = true;
                document.getElementById("rdtwo").checked = false;
                document.getElementById("rdthree").checked = false;
            }
            else if(button == "two")
            {
                document.getElementById("rdone").checked = false;
                document.getElementById("rdtwo").checked = true;
                document.getElementById("rdthree").checked = false;   
            }
            else if(button == "three")
            {
                alert("3");
                document.getElementById("rdone").checked = false;
                document.getElementById("rdtwo").checked = false;
                document.getElementById("rdthree").checked = true;
                alert("2");
            }
            else
            {
                    alert("4");
            }
        </script>

            <input id="rdone" type="radio" name="radio" value="one"/>
            <input id="rdtwo" type="radio" name="radio" value="two"/>
            <input id="rdthree" type="radio" name="radio" value="three"/>
            <input type="submit" value="Submit"/>
        </form>
    </body>
</html>

和Servlet。

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try 
        {
            System.out.println(request.getParameter("radio"));
            RequestDispatcher dispatcher = request.getRequestDispatcher("/radiobutton.jsp");
            System.out.println("--------------"+request.getParameter("radio"));
            dispatcher.forward(request, response);
        } 
        finally 
        {            
            out.close();
        }

    }
一切正常。但我得到java脚本错误。请检查以下屏幕截图。因为这个java脚本我没有得到确切的输出。

enter image description here

2 个答案:

答案 0 :(得分:1)

您的脚本出现在单选按钮之前,并且不包含延迟到稍后的功能。

您尝试在按钮存在之前对其进行修改。

将脚本移动到HTML源代码中的按钮之后。

答案 1 :(得分:0)

我想建议您在提交JSP页面时,可以使用以下代码在servlet上处理它:

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try 
            {
                System.out.println(request.getParameter("radio"));
if(request.getParameter("radio")!= "" || request.getParameter("radio")!=null){
 request.setAttribute("radio");
}
                RequestDispatcher dispatcher = request.getRequestDispatcher("/radiobutton.jsp");
                System.out.println("--------------"+request.getParameter("radio"));
                dispatcher.forward(request, response);
            } 
            finally 
            {            
                out.close();
            }

        }

现在在JSP文件中添加以下行:

<input id="rdone" type="radio" name="radio" value="one" <%=((request.getAttribute("radio")!=null && request.getAttribute("radio").toString().equals("one")) ? "checked" : "")%> />
<input id="rdtwo" type="radio" name="radio" value="two" <%=((request.getAttribute("radio")!=null && request.getAttribute("radio").toString().equals("two")) ? "checked" : "")%> />
<input id="rdthree" type="radio" name="radio" value="three" <%=((request.getAttribute("radio")!=null && request.getAttribute("radio").toString().equals("three")) ? "checked" : "")%> />