一个简单的Web登录程序 GET和POST的区别
JSP程序
首先,写一个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 'login.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>
<form action="Login">
username: <input type="text" name="username"><br>
password: <input type="password" name="password"><br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>
注意action中是web.xml中的url-pattern,(这里给出相对路径),这样,提交之后才能启动相应的Servlet程序。
Servlet程序
然后,写一个Servlet程序,将收到的数据显示出来:
package com.shengqishiwind.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
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
processLogin(req, resp);
}
private void processLogin(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
String username = req.getParameter("username");
String password = req.getParameter("password");
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html><head><title>Login Result</title></head>");
out.println("<body> username: " + username + "<br>");
out.println("password: " + password + "</body></html>");
out.flush();
}
}
web.xml
在web.xml中建立好映射关系:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<servlet>
<servlet-name>LoginResult</servlet-name>
<servlet-class>com.shengqishiwind.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginResult</servlet-name><!-- servlet-name 要和上面的保持一致-->
<url-pattern>/Login</url-pattern><!-- 注意前面要有斜线-->
</servlet-mapping>
</web-app>
程序运行
首先,需要在服务器的server.xml中配置好路径。Host标签中:
<Context path="/HelloLogin" docBase="E:MDDMyEclipseWorkspaceHelloLoginWebRoot" reloadable="true"/>
然后,启动Tomcat服务器(运行startup.bat)。
在浏览器中输入:http://localhost:8080/HelloLogin/login.jsp
显示登录页面:
输入后点击submit,进入下一个页面:
注意此时浏览器地址栏中显示:http://localhost:8080/HelloLogin/Login?username=shengqishi&password=wind
get和post方法
上面,浏览器中地址栏显示的是:
http://localhost:8080/HelloLogin/Login?username=shengqishi&password=wind
这样岂不是直接将用户名和密码暴露出来了?
在jsp的form表单中,有这样一个属性:method,它的默认值是get,如果将它设置为post,则提交表单后,地址栏不会有用户名和密码的显示。
但是Servlet程序中需要提供doPost()的实现。
改动如下:
将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 'login.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>
<form action="Login" method="post">
username: <input type="text" name="username"><br>
password: <input type="password" name="password"><br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
</body>
</html>
Servlet程序改为:
package com.shengqishiwind.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
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
processLogin(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
processLogin(req, resp);
}
private void processLogin(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
String username = req.getParameter("username");
String password = req.getParameter("password");
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html><head><title>Login Result</title></head>");
out.println("<body> username: " + username + "<br>");
out.println("password: " + password + "</body></html>");
out.flush();
}
}
再次启动浏览器,输入:http://localhost:8080/HelloLogin/login.jsp
提交后,地址栏中显示的是:http://localhost:8080/HelloLogin/Login
后面没有用户名和密码信息。
这是因为POST请求将请求参数不是作为请求URL的一部分,而是将请求参数作为请求体的一部分(两个回车之后附加请求参数)。
POST方法向服务器发送请求,要求服务器接受附在后面的数据。POST方法在表单提交的时候用的最多。
总结:
get与post方法之间的差别:
1.浏览器地址栏呈现的结果不同(表象);
2.真正的原因在于向服务器端发送请求时的形式是不同的。
3.get的请求格式:
GET /test/LoginServlet?username=hello&password=world HTTP/1.1
4.post的请求格式:
POST /test/LoginServlet HTTP/1.1
Connection: Keep-Alive
username=hello&password=world
5.通过浏览器进行文件上传时,一定要使用post方式而绝不能使用get方式。
6.通过浏览器地址栏输入网址的方式来访问服务器端资源,全部使用的是get方法请求的。