package com.xzit.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class UrlRewriteServlet
*/
@WebServlet("/servlet/UrlRewriteServlet")
public class UrlRewriteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("username");
String pwd = request.getParameter("password");
if(name.equals("admin") && pwd.equals("111111")) {
HttpSession session = request.getSession();
session.setAttribute("username", name);
String sessId = session.getId(); //获取会话的唯一标识
System.out.println("当前会话的id:"+sessId);
String newUrl = "welcome.jsp"; //被重写的url
String rewriteUrl = response.encodeRedirectURL(newUrl);
/* 获取当前应用程序的部署发布url全名称 */
String http = request.getScheme();
String ip = request.getServerName();
int port = request.getServerPort();
String path = request.getContextPath();
String contextFullPath = http+"://"+ip+":"+port+path+"/";
System.out.println("发布应用程序全名称:"+contextFullPath);
//request.getRequestDispatcher(contextFullPath+rewriteUrl).forward(request, response);
response.sendRedirect(contextFullPath+"jsp/"+rewriteUrl);
}else {
response.sendRedirect("index.jsp?message=failed&info=login failed");
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
<%
String m = request.getParameter("message"
);
String info = request.getParameter("info"
);
if(m !=
null && m.equals("failed"
)){
%>
系统提示:<%=info %>
<%
}
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
当前用户名称:${sessionScope.username}
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
UrlRewrite
5
index.jsp