前言
本作业为当堂作业,时间为20 min.
一、题目要求
- New a web project, write addUser.html to fold “WebRoot”.
- New a servlet to get all data submitted from the form in
addUser.html, and display data in console. - 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->user management->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> </td>
<td>user name:
<label>
<input name="username" type="text" id="username">
</label></td>
<td> </td>
</tr>
<tr>
<td> </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> </td>
</tr>
<tr>
<td> </td>
<td>Real Name:
<label>
<input name="realname" type="text" id="realname">
</label></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>sex:
<label>
<select name="sex" id="sex">
<option value="1">male</option>
<option value="0">female</option>
</select>
</label></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><label>
<input type="submit" name="Submit" value="submit">
</label></td>
<td> </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