用MyEclipse编写一个Servlet处理表单的程序(小白进阶)

程序实现功能如图所示:

用MyEclipse编写一个Servlet处理表单的程序(小白进阶)

用MyEclipse编写一个Servlet处理表单的程序(小白进阶)

1.首先启动Tomcat7.0服务(双击.exe文件)

2.打开MyEclipse——>选择Window选项下——>Preferences——>MyEclipse

          ——>Servers——>Tomcat——>Tomcat7.x进行设置

用MyEclipse编写一个Servlet处理表单的程序(小白进阶)

3.打开MyEclipse——>new——>Web Project进行新建web项目文件

用MyEclipse编写一个Servlet处理表单的程序(小白进阶)

4.在testone右击新建——>package建包sun.hebtu——>包上右击新建Servlet命名为servletone

5.打开testone——>WebRoot——>index.jsp文件打开进行jsp页面的设计

在<body></body>体中进行主要代码编写,实现创建表单功能,以下是index.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>
               <h1>潜在用户网络调查</h1><br>
    <form action="servletone" method="get"> 
        姓名:<input name="name" type="text" size="22" /> <br><br>
        EMAIL:<input name="email" type="text" size="22" /><br>
        年纪:
            <input name="age" type="radio" value="小于18"  checked=checked/>小于18
            <input name="age" type="radio" value="18-25" />18-25
            <input name="age" type="radio" value="26-40 " />26-40 
            <input name="age" type="radio" value="大于40" />大于40 <br><br>
        编程时间:
            <select name="time"> 
                <option value="0-5月">0-5月 
                <option value="6-12月">6-12月
            </select> <br><br>
        使用的操作系统:
            <select name="system" size=6 multiple> 
                <option value="Win XP">Win XP
                <option value="Win 2000/2003">Win 2000/2003
                <option value="Linux">Linux
                <option value="FreeBSD">FreeBSD
                <option value="Mac OS">Mac OS
                <option value="other">other
            </select> <br><br>
        使用的编程语言:
            <input name="language" type="checkbox" value="C" />C
            <input name="language" type="checkbox" value="C++" />C++
            <input name="language" type="checkbox" value="C#" />C#
            <input name="language" type="checkbox" value="PYTHON" />PYTHON
            <input name="language" type="checkbox" value="JAVA" />JAVA
            <input name="language" type="checkbox" value="VB" />VB
            <input name="language" type="checkbox" value="DEPHI" />DEPHI
            <br><br>
        建议:
            <textarea name="device" cols="30" rows="4"> 
               </textarea>  <br><br>
        <input name="tj" type="submit" value="提交" /> 
        <input name="cz" type="reset" value="重置" />    
  </body>
</html>

6.右击Tomcat7.x——>Add Deployment——>添加testone项目

7.打开web.xml文件查看代码可以将/servlet删除哦

用MyEclipse编写一个Servlet处理表单的程序(小白进阶)

8.下面我们需要在servletone.java中获取页面中表单数据

      *我们希望doGet和doPost完成同样的功能便在doGet中调用doPost方法

      *在doPost方法中获得表单数据

     下面是servletone.java中完整代码

package sun.hebtu;

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 servletone extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public servletone() {
        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 {
            //现在我们希望doGet和doPost实现一样的功能,在doGet中调用doPost方法。
            doPost(request, response);
    }

    /**
     * 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 {
        //进行字符编码及输出格式的设置
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=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("name");
        String email=request.getParameter("email");
        String age=request.getParameter("age");
        String time=request.getParameter("time");    
        String system1=request.getParameter("system");
        String language1=request.getParameter("language");
        String device=request.getParameter("device");
        //输出格式及内容
        out.println("姓名:"+name+"<br>");
        out.println("EMAIL:"+email+"<br>");
        out.println("年纪:"+age+"<br>");
        out.println("编程时间:"+time+"<br>");
        out.println("使用的操作系统:");
        String itemName[]=request.getParameterValues("system");
        if(itemName==null){
            out.println("");
        }
        else{
            for(int i=0;i<itemName.length;i++){
                out.print(" "+itemName[i]);
            }
        }
        out.println("<br>");
        out.println("使用的编程语言:"); //
        String itemName2[]=request.getParameterValues("language");
        if(itemName2==null){
            out.println("");
        }
        else{
            for(int i=0;i<itemName2.length;i++){
                out.print(" "+itemName2[i]);
            }
        }
        out.println("<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
    }

}

9.现在可以打开ie浏览器输入http://localhost:8081/testone/即可查看表单页面

进行信息输入后点击确定按钮完成跳转

用MyEclipse编写一个Servlet处理表单的程序(小白进阶)

 

用MyEclipse编写一个Servlet处理表单的程序(小白进阶)

10.现在功能已经完成,你运行成功了么

注意:如果在运行过程中出现乱码,请检查字符编码,具体查看内容请看另一篇博客:

https://blog.****.net/lmm0513/article/details/88325860