我是servlet的新手。我正在创建一个小型动态Web应用程序。我想创建一个登录和注销模块,只有在正确登录后才能通过该模块访问其他HTML或JSP页面。我借助一些在线资源创建了一个简单的登录和注销HTML及其servlet。但是,如果我希望成功登录后只有该用户可以访问其他HTML或JSP页面,该怎么办?直接点击URL网址可能无法访问相同的JSP或HTML页面。
在下面的项目结构中,有一些JSP和HTML页面。假设我不想让用户在没有正确登录的情况下直接打开ola.html和ola_create.jsp,那么我应该如何使其工作。请在下面查看我的项目结构:
以下是该servlet的代码:
LoginServlet.java
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;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
String name=request.getParameter("name");
String password=request.getParameter("password");
if(password.equals("admin123")){
//response.sendRedirect("ola.html");
// out.print("Welcome, "+name);
HttpSession session=request.getSession();
session.setAttribute("name",name);
}
else{
out.print("Sorry, username or password error!");
request.getRequestDispatcher("login.html").include(request, response);
}
out.close();
}
}
LogoutServlet.java
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;
import javax.servlet.http.HttpSession;
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
HttpSession session=request.getSession();
session.invalidate();
out.print("You are successfully logged out!");
out.close();
}
}
答案 0 :(得分:0)
在这种情况下,您可以使用Servlet过滤器。它主要用于执行转换,日志记录,压缩,加密和解密,输入验证等过滤任务。
因此,在访问资源(例如servlet,JSP或HTML页面)之前,基本上就像一个验证层。例如,如果您使用url-mapping / hello创建过滤器,那么对于/您好,第一个过滤器将被执行。如果验证失败,则可以验证用户是否已登录此过滤器,否则将不允许用户访问/ hello请求。
您可以在web.xml文件中定义类似的内容。
<filter>
<filter-name>f1</filter-name>
<filter-class>MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/ola</url-pattern>
</filter-mapping>
这是您定义过滤器的方式
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
public class MyFilter implements Filter{
public void init(FilterConfig arg0) throws ServletException {}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
PrintWriter out=resp.getWriter();
out.print("filter is invoked before");
String user=request.getParameter("username");
if(user=="admin"){
chain.doFilter(req, resp);//sends request to next resource
}
else {
out.print("You are not authorized to view this page");
}
}
public void destroy() {}
}
附录-