I am new to web programming. I am asking a common pattern to do things like checking authentication. Here is the scenario:
The website has a login page for visitors. It will take username and encrypted password and sent them to server, then get either a error code (username/password doesn't match)or an auth key from the server. When the user logged in successfully, I want the website automatically jump to the main.jsp page that presents the main functionality of the website. 
In this case, I want main.jsp check the user authentication. That is, I don't want such thing happens like user can directly open www.example.com/main.jsp, and if they did thing like this, I want to redirect them to login page. 
So how could I pass authentication information across page, and how could I prevent user from directly accessing the main.jsp without login? Do I need to use session or anything?
setAttribute("loggedInUser", user); ... } Or in your JSP page you can check if the user is logged in using JSTL tags as showed by BalusC in the example here: ... <c:if test="${not empty loggedInUser}"> <p>You're still logged in.
The getAuthType() method returns a String object that represents the name of the authentication scheme used to protect the Servlet.
JSP architecture is a 3 tier architecture. It has a Client, Web Server, and Database. The client is the web browser or application on the user side. Web Server uses a JSP Engine i.e; a container that processes JSP.
Role Based Authentication technique used for authentication mechanism in servlet specification - JSP.
you could try using filters:
Filter can pre-process a request before it reaches a servlet, post-process a response leaving a servlet, or do both. Filters can intercept, examine, and modify requests and responses.
NOTE: be sure to add a session attribute once your user is logged in, you can use that session attribute on the filter
on your login.jsp add:
session.setAttribute("LOGIN_USER", user); 
//user entity if you have or user type of your user account... 
//if not set then LOGIN_USER will be null
web.xml
<filter>
    <filter-name>SessionCheckFilter</filter-name>
    <filter-class>yourjavapackage.SessionCheckFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SessionCheckFilter</filter-name>
    <!--url-pattern>/app/*</url-pattern-->
    <url-pattern>/main.jsp</url-pattern> <!-- url from where you implement the filtering -->
</filter-mapping>
SessionCheckFilter.java
public class SessionCheckFilter implements Filter {
  private String contextPath;
  @Override
  public void init(FilterConfig fc) throws ServletException {
    contextPath = fc.getServletContext().getContextPath();
  }
  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;  
    if (req.getSession().getAttribute("LOGIN_USER") == null) { //checks if there's a LOGIN_USER set in session...
        res.sendRedirect(contextPath + "/login.jsp"); //or page where you want to redirect
    } else {
      String userType = (String) req.getSession().getAttribute("LOGIN_USER");
      if (!userType.equals("ADMIN")){ //check if user type is not admin
        res.sendRedirect(contextPath + "/login.jsp"); //or page where you want to  
      }
      fc.doFilter(request, response);
    }
  }
  @Override
  public void destroy() {
  }
}
                        How JSP page should check authentication
It shouldn't. You should use Container Managed Authentication, and define the login/security in web.xml via URL patterns.
Added by Glen Best:
E.g. Add something like this to web.xml:
<security-constraint>
   <display-name>GET: Employees Only</display-name>
   <web-resource-collection>
      <web-resource-name>Restricted Get</web-resource-name>
      <url-pattern>/restricted/employee/*</url-pattern>
      <http-method>GET</http-method>
   </web-resource-collection>
   <auth-constraint>
      <role-name>Employee</role-name>
   </auth-constraint>
   <user-data-constraint>
      <transport-guarantee>CONFIDENTIAL</transport-guarantee>
   </user-data-constraint>
</security-constraint>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With