我使用單選按鈕提交表單。我希望在提交表單後檢查單選按鈕。基本上我正在頁面上進行一些搜索過程。請檢查代碼。在jsp/servlet中提交表單後的單選按鈕選擇
在我的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腳本我沒有得到確切的輸出。
* *危險**:您正在將URL中的數據直接輸出到頁面中。這使你容易受到[XSS攻擊](http://en.wikipedia.org/wiki/Cross-site_scripting)的影響。 – Quentin 2013-04-11 10:05:57