转发与重定向学习代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了转发与重定向学习代码相关的知识,希望对你有一定的参考价值。
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+"光临");
}
}
以上是关于转发与重定向学习代码的主要内容,如果未能解决你的问题,请参考以下文章