前台页面:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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 'data.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">
</head>
<body>
<form action="servlet/getdata" method="post">
专业:<select name="zy">
<option value="计算机科学与技术">计算机科学与技术</option>
<option value="软件工程">软件工程</option>
<option value="网络工程">网络工程 </option>
</select><br>
班级:<input type="text" name="bj" size="10"><br>
姓名:<input type="text" name="xm" size="10"><br>
性别:<input type="radio" name="xb" value="男">男<input type="radio" name="xb" value="女">女<br>
专业课程(可多选):<br>
<select name="course" size=4 multiple=true>
<option value="J2EE架构">J2EE架构</option>
<option value=".NET架构">.NET架构</option>
<option value=".NET架构">Java程序设计</option>
<option value="C程序设计">C程序设计</option>
</select><br><br>
<input type="submit" value="提交">
<input type="reset" value="重填">
</form>
</body>
</html>
后台servlet:
package servlets;
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 getdata extends HttpServlet {
/**
* 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>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @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 doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
String zy=new String((request.getParameter("zy")).getBytes("ISO-8859-1"));
String bj=new String((request.getParameter("bj")).getBytes("ISO-8859-1"));
String xm=new String((request.getParameter("xm")).getBytes("ISO-8859-1"));
String xb=new String((request.getParameter("xb")).getBytes("ISO-8859-1"));
String[] ss= request.getParameterValues("course");
String course="";
for(String s:ss){
course+=s+"、";
}
out.println("专业:"+zy+"<br>");
out.println("班级:"+bj+"<br>");
out.println("姓名:"+xm+"<br>");
out.println("性别:"+xb+"<br>");
out.println("专业课程:"+new String(course.getBytes("ISO-8859-1"))+"<br>");
out.flush();
out.close();
}
}