Jsp表单常用标签get和post方式处理
1 页面如下:
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true"%>
<html>
<head>
<title>
表单的测试
</title>
<script language="javascript">
function getsubmit()
{
var username="username="+document.getElementById("username").value;//第一行的元素
var sexvalue = document.getElementsByName("usersex");
var turesexvalue;//因为sexvalue有多个值,所以需要更多的判断。
for(i=0;i<sexvalue.length;i++)
{
if(sexvalue[i].checked)//判断哪个值被选中
{
turesexvalue=sexvalue[i].value;//获得被选中的值
}
}
var usersex= "&usersex="+turesexvalue;//第二行的元素
var hobbyvalue=document.getElementsByName("hobby");
var checkhobby="";
for(i=0;i<hobbyvalue.length;i++)
{
if(hobbyvalue[i].checked)//判断哪个值被选中
{
checkhobby=checkhobby+hobbyvalue[i].value+"|";//获得被选中的值,并累加
}
}
var hobby="&hobby="+checkhobby;//第三行的元素
var nation = "&nation="+document.getElementById("nation").value;//第四行的元素
var descript="&descript="+document.getElementById("descript").value;
//把要提交的控件用字符串的形式拼在提交的方法
var urls = "getgo?"+username+usersex+hobby+nation+descript;
alert(urls);
window.location=urls;
}
</script>
</head>
<body>
<form action="getgo" method="post">
<table border="2">
<tr>
<td colspan="2">
个人信息
</td>
</tr>
<tr>
<td>
姓名:
</td>
<td>
<input type="text" name="username" size="30">
</td>
</tr>
<tr>
<td>
性别:
</td>
<td>
男<input type="radio" name="usersex" value="male" checked/>
女<input type="radio" name="usersex" value="female"/>
</td>
</tr>
<tr>
<td>
爱好:
</td>
<td>
篮球<INPUT TYPE="checkbox" name="hobby" value="baskball" >
足球<INPUT TYPE="checkbox" name="hobby" value="football" >
</td>
</tr>
<tr>
<td>
民族:
</td>
<td>
<select name="nation" clientTop="99">
<option value="汉族">汉族</option>
<option value="满族">满族</option>
<option value="回族">回族</option>
</select>
</td>
</tr>
<tr>
<td>
个人说明:
</td>
<td>
<textarea name="descript" rows="5" cols="15"></textarea>
</td>
</tr>
<tr>
<td>
<input type="button" name="button1" value="tijiao" onclick="getsubmit()">
<input type="submit" name="submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
2web.xml配置
<servlet>
<servlet-name>get</servlet-name>
<servlet-class>first.GetJsp</servlet-class>
</servlet>
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>first.HelloWorld</servlet-class>
</servlet>
3 servlet类
package first;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetJsp extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
System.out.println(username);
this.getServletContext().getRequestDispatcher("/finish.jsp")
.forward(request, response);
//response.sendRedirect("finish.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String frontpage = request.getParameter("username");
String hobby[] = request.getParameterValues("hobby");
for(int i=0;i<hobby.length;i++)
System.out.println(hobby[i]);
System.out.println(frontpage);
this.getServletContext().getRequestDispatcher("/finish.jsp")
.forward(request, response);
//response.sendRedirect("finish.jsp");
}
}