1. 程式人生 > >轉發與重定向學習代碼

轉發與重定向學習代碼

轉發與重定向學習代碼

html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用戶登錄</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <form action="/day07/LoginServlet" method="post">
        <table border="1" align="center">
            <caption>用戶登錄</caption>
            <tr>
                <th>用戶名</th>
                <td><input type="text" name="username"/></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="提交"/>
                        ????
                    <input type="reset" value="重填"/>
                </td>
            </tr>   
        </table>
    </form>
  </body>
</html>

java

LoginServlet

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.web.domain.IpCheckBean;

public class LoginServlet extends HttpServlet {
    //key為IP,value為歸屬地
    private Map<String,String> map = new HashMap<String,String>();
    public void init() throws ServletException {
        //在用戶第一次訪問前加載IP和歸屬地,不存在多線程安全問題
        map.put("192.168.12.222","北京");
        map.put("192.168.11.122","上海");
        map.put("192.168.11.50","深圳");
        map.put("192.168.11.33","佛山");
        map.put("192.168.11.44","珠海");
        map.put("127.0.0.1","北京");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //IP
        String ip = request.getRemoteAddr();
        IpCheckBean ipCheckBean = new IpCheckBean();
        //歸屬地
        String address = ipCheckBean.check(ip,map);
        //用戶名
        String username = request.getParameter("username");
        //將信息綁定到ServletContext對象中
        ServletContext context = this.getServletContext();
        context.setAttribute("username",username);
        context.setAttribute("ip",ip);
        context.setAttribute("address",address);
        /*轉發到ShowServlet
        context
            .getRequestDispatcher("/ShowServlet")
            .forward(request,response);
        */
        //重定向到ShowServlet  是走get方法
        response.sendRedirect("/day07/ShowServlet");
    }
}

ShowServlet

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = response.getWriter();
        pw.write("歡迎:"+context.getAttribute("username")+"光臨<br/>");
        pw.write("你的IP是:"+context.getAttribute("ip")+"<br/>");
        pw.write("歸屬地:"+context.getAttribute("address")+"<br/>");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        this.doPost(request,response);
    }
}

ListServlet

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 ListServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String username = (String) request.getAttribute("USERNAME");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("歡迎"+username+"光臨");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String username = (String) request.getAttribute("USERNAME");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("歡迎"+username+"光臨");     
    }
}

轉發與重定向學習代碼