1、运用java语言,页面编码格式:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2、form表单,但你提交后,会跳转到servlet/Studentservlet,采用的方式是post,还有另一种方式,是get,一般用post,如果不写则默认为get:
<form action="servlet/Studentservlet" method="post">
<h2>学生信息收集:</h2>
姓名:<input type="text" name="uname"><br /> <br />
身份证号:<input name="sfz" /><br /> <br />
职业:<input type="text" name="uwork"><br /> <br />
性别:
<input type="radio" name="gender" value="男" />男
<input type="radio" name="gender" value="女" />女
<br /> <br />
国籍:
<select name="nana">
<option value="中国">中国</option>
<option value="委内瑞拉">委内瑞拉</option>
<option value="美国">美国</option>
<option value="朝鲜">朝鲜</option>
</select>
<br /> <br />
所在地:
<select name="szd">
<option value="重庆">重庆</option>
<option value="上海">上海</option>
<option value="北京">北京</option>
<option value="成都">成都</option>
<option value="广州">广州</option>
<option value="厦门">厦门</option>
<option value="福建">福建</option>
<option value="澳门">澳门</option>
</select>
<br /><br />
文凭:
<select name="wp">
<option value="小学">小学</option>
<option value="初中">初中</option>
<option value="高中">高中</option>
<option value="大学">大学</option>
<option value="博士">博士</option>
<option value="硕士">硕士</option>
</select>
<br /><br />
联系方式:<input name="phone" /><br /> <br />
<br />
<br />
<input type="submit" value="提交信息" />
</form>
3、用dopost函数模块时在网页显示出现中文乱码的处理方式:
response.setCharacterEncoding("utf-8");
4、用doget函数模块时在网页显示出现中文乱码的处理方式:
String name = request.getParameter("uname");
name = new String(name.getBytes("iso-8859-1"), "utf-8");
5、完整代码
(1).jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="servlet/Studentservlet" method="post">
<h2>学生信息收集:</h2>
姓名:<input type="text" name="uname"><br /> <br />
身份证号:<input name="sfz" /><br /> <br />
职业:<input type="text" name="uwork"><br /> <br />
性别:
<input type="radio" name="gender" value="男" />男
<input type="radio" name="gender" value="女" />女
<br /> <br />
国籍:
<select name="nana">
<option value="中国">中国</option>
<option value="委内瑞拉">委内瑞拉</option>
<option value="美国">美国</option>
<option value="朝鲜">朝鲜</option>
</select>
<br /> <br />
所在地:
<select name="szd">
<option value="重庆">重庆</option>
<option value="上海">上海</option>
<option value="北京">北京</option>
<option value="成都">成都</option>
<option value="广州">广州</option>
<option value="厦门">厦门</option>
<option value="福建">福建</option>
<option value="澳门">澳门</option>
</select>
<br /><br />
文凭:
<select name="wp">
<option value="小学">小学</option>
<option value="初中">初中</option>
<option value="高中">高中</option>
<option value="大学">大学</option>
<option value="博士">博士</option>
<option value="硕士">硕士</option>
</select>
<br /><br />
联系方式:<input name="phone" /><br /> <br />
<br />
<br />
<input type="submit" value="提交信息" />
</form>
</body>
</html>
(2).java文件:
package login;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Studentservlet extends HttpServlet {
/**
* Constructor of the object.
*/
public Studentservlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
//处理post提交中文乱码
request.setCharacterEncoding("utf-8");
//获取信息
String name = request.getParameter("uname");
name = new String(name.getBytes("iso-8859-1"), "utf-8");
String sfz = request.getParameter("sfz");
String work = request.getParameter("uwork");
work = new String(work.getBytes("iso-8859-1"), "utf-8");
String gender = new String(request.getParameter("gender").getBytes("iso-8859-1"),"utf-8");
String nana = new String(request.getParameter("nana").getBytes("iso-8859-1"),"utf-8");
String szd = new String(request.getParameter("szd").getBytes("iso-8859-1"),"utf-8");
String wp = new String(request.getParameter("wp").getBytes("iso-8859-1"),"utf-8");
String phone = request.getParameter("phone");
out.println(name + "<br />");
out.println(sfz + "<br />");
out.println(work + "<br />");
out.println(gender + "<br />");
out.println(nana + "<br />");
out.println(szd + "<br />");
out.println(wp + "<br />");
out.println(phone + "<br />");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
//处理post提交中文乱码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
//获取信息
String name = request.getParameter("uname");
String sfz = request.getParameter("sfz");
String work = request.getParameter("uwork");
String gender = request.getParameter("gender");
String nana = request.getParameter("nana");
String szd = request.getParameter("szd");
String wp = request.getParameter("wp");
String phone = request.getParameter("phone");
out.println(name + "<br />");
out.println(sfz + "<br />");
out.println(work + "<br />");
out.println(gender + "<br />");
out.println(nana + "<br />");
out.println(szd + "<br />");
out.println(wp + "<br />");
out.println(phone + "<br />");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}