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+"光临"); }}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦