Servlet 作业

本文介绍了一个简单的用户添加表单实现,并通过Servlet接收表单数据并打印到控制台的过程。具体包括addUser.html页面设计、Login.java的doPost方法实现及web.xml配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言
本作业为当堂作业,时间为20 min.

一、题目要求

  1. New a web project, write addUser.html to fold “WebRoot”.
  2. New a servlet to get all data submitted from the form in
    addUser.html, and display data in console.
  3. You need to configure form’s action parameter in addUser.html, so
    that form data will be submitted to the servlet you created.

效果如下图所示

addUser.html:
效果

console:

输出

二、实践
我编写了三个文件,addUser.html , web.xml和 login.java

干货呈上:
addUser.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <title>add user</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>
  <p>  system admin-&gt;user management-&gt;add user  </p>
   <form name="addUserform" method="post" action="servlet/Login">
    <input name="operation" type="hidden" id="operation" value="add">
     <table width="95%" height="389" border="0">
       <tr>
         <td>&nbsp;</td>
         <td>user name:
           <label>
           <input name="username" type="text" id="username">
         </label></td>
         <td>&nbsp;</td>
       </tr>
       <tr>
         <td>&nbsp;</td>
         <td>user type:
           <label>
           <select name="userType" id="userType">
             <option  value="2">Administrator</option>
             <option  value="1">Teacher</option>
             <option  value="0">Student</option>
           </select>
         </label></td>
         <td>&nbsp;</td>
       </tr>
       <tr>
         <td>&nbsp;</td>
         <td>Real Name:
           <label>
           <input name="realname" type="text" id="realname">
         </label></td>
         <td>&nbsp;</td>
       </tr>
       <tr>
         <td>&nbsp;</td>
         <td>sex:
           <label>
           <select name="sex" id="sex">
             <option  value="1">male</option>
             <option  value="0">female</option>
           </select> 
         </label></td>
         <td>&nbsp;</td>
       </tr>
       <tr>
         <td>&nbsp;</td>
         <td>&nbsp;</td>
         <td>&nbsp;</td>
       </tr>
       <tr>
         <td>&nbsp;</td>
         <td><label>
           <input type="submit" name="Submit" value="submit">
         </label></td>
         <td>&nbsp;</td>
       </tr>
     </table>
  </form>
   <p><br>
    </p>
  </body>
</html>

login.java

package servlet;

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

    /**
     * Constructor of the object.
     */
    public Login() {
        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>");
        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");
        String username=request.getParameter("username");
        String usertype=request.getParameter("userType");
        String realname=request.getParameter("realname");
        String sex=request.getParameter("sex");

        System.out.println("userType= "+usertype);
        System.out.println("username= "+username);
        System.out.println("realname= "+realname);
        System.out.println("sex= "+sex);



        /**
        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 POST method");
        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
    }

}

web.xml

<servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Login</servlet-name>
    <servlet-class>servlet.Login</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/servlet/Login</url-pattern>
  </servlet-mapping>    
  <welcome-file-list>
    <welcome-file>addUser.html</welcome-file>
  </welcome-file-list>

三、效果如下

INFO: Server startup in 27097 ms
userType= 1
username= yourname
realname= Frist name Last Name
sex= 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值