适逢下雨,在实验室无聊,冥想中闪过一个很久之前就想做的一个小实验,对于form表单中的文本框,submit之后,后台获取其中的参数值,是根据文本框的id来获取,还是根据文本框的name来获取呢?一个很简单的小问题,所以随手写了一个demo来测试下,测试代码如下。
首先,新建一个jsp页面: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>
This is my JSP page. <br>
<form id="form1" method="post" action="servlet/LoginServlet">
姓名:<input type="text" id="name" name="username"/><br/>
密码:<input type="password" id="pwd" name="password"/><br/>
<input type="submit" id="submitForm1"/>
</form>
</body>
</html>
然后,新建servlet:LoginServlet
package com.js.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 LoginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
public LoginServlet() {
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 {
System.out.println(request.getParameter("username"));
System.out.println(request.getParameter("password"));
System.out.println("------------------------------");
System.out.println(request.getParameter("name"));
System.out.println(request.getParameter("pwd"));
}
/**
* 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 {
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
最后,配置下web.xml(注:如果新建的时候选择servlet,myeclipse会自动配置,如果新建的是java类,则需要自己手动配置):
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<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>LoginServlet</servlet-name>
<servlet-class>com.js.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/LoginServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这里注意下:
url-pattern是:/servlet/LoginServlet
而jsp页面中的action值是:servlet/LoginServlet
发布项目,并运行。结果如下。
结果表明,后台的servlet获取form表单传递的参数时,是根据name字符串来获取的,而不是id字符串,form表单发送参数到后台时,其报文中发送的参数名是根据name属性来设置的。
为了进一步验证这一点,我们使用谷歌浏览器来访问该项目,并F12-network来查看htt报文:
验证完毕,在今后的学习中,我会继续秉持“知其然知其所以然”的态度,进一步扎实自己的基本功,不断深入理解原理。