Servlet Get Post

Servlet前台数据提交不管是get还是post,都会先进入service进行处理。如果重写service方法,则不会再处理doGet或doPost。可以在service方法体结尾加上super.service(req, resp);(其实也就是利用基类的默认方法)。
package com.my.action;  
  
import java.io.IOException;  
import java.io.PrintWriter;  
  
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;
  
public class ServletTest extends HttpServlet {  
    private static final long serialVersionUID = -3070515574790298884L;  
  
    @Override  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
  
        System.out.println("Get");  
        this.doBusiness(req, resp);  
    }  
  
    @Override  
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
  
        System.out.println("Post");  
        this.doBusiness(req, resp);  
  
    }  
  
    @Override  
    protected void service(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        String method = req.getMethod();  
        System.out.println("method: " + method);  
          
        /*if(method.equals("get")){ 
            this.doGet(req, resp); 
        }  
        if(method.equals("post")){ 
            this.doPost(req, resp); 
        } */  
        super.service(req, resp);  
    }  
  
    protected void doBusiness(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
  
        String username = req.getParameter("username");  
        String password = req.getParameter("password");  
  
        PrintWriter printWrite = resp.getWriter();  
        printWrite.println("Hello Post");  
  
        printWrite.println(username);  
        printWrite.println(password);  
        
        HttpSession session = req.getSession();
        session.setAttribute("username", username);
        session.setAttribute("password", password);
        
        /*req.setAttribute("username", username);
        req.setAttribute("password", password);*/
        
        /**只能在url中带parameter或者放在session中,无法使用request.setAttribute来传递*/
        resp.sendRedirect("page/success.jsp");
        
        /**使用这种方式跳转,传值可以使用三种方法:url中带parameter,session,request.setAttribute
        跳转后浏览器地址栏不会变化*/
        /*RequestDispatcher dispatcher = req.getRequestDispatcher("/page/success.jsp");
        dispatcher.forward(req, resp);*/
  
    }  
  
}  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>servletTest</display-name>
  
  <servlet>
  	<servlet-name>ServletTest</servlet-name>
  	<servlet-class>com.my.action.ServletTest</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>ServletTest</servlet-name>
  <url-pattern>/ST</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<% %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath}/ST" method="post">
		<table>
			<tr>
				<td>Username: <input type="text" name="username" /></td>
				<td>Password: <input type="text" name="password" /></td>
			</tr>
			<tr>
				<td><input type="submit" value="Submit" /></td>
				<td><input type="reset" value="Reset"/></td>
			</tr>
		</table>
	</form>
</body>
</html>

<%@ 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 'success.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>
    <table>
    	<tr>
    		<td>Username : ${username }</td>
    		
    		<td>Password : ${password }</td>
    	</tr>
    </table>
  </body>
</html>


***********************************************************

1. get是从服务器上获取数据,post是向服务器传送数据。
2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。post是通过HTTP post机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。
3. 对于get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。
4. get传送的数据量较小,不能大于2KB。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为100KB。
5. get安全性非常低,post安全性较高。但是执行效率却比Post方法好。

建议:
1、get方式的安全性较Post方式要差些,包含机密信息的话,建议用Post数据提交方式;
2、在做数据查询时,建议用Get方式;而在做数据添加、修改或删除时,建议用Post方式

Servlet 跳转 redirect与forward跳转的区别
Servlet doGet和doPost方法

你可能感兴趣的:(servlet)