home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java EE 6 example source code file (LoginServlet.java)

This example Java EE 6 source code file (LoginServlet.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Java EE 6 tags/keywords

after, failed, http, httpservletresponse, ioexception, ioexception, isuserinrole, isuserinrole, login, logout, net, network, request, response, servlet, servletexception, servletexception, short, string, string

The Java EE 6 LoginServlet.java source code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enterprise.programmatic_login;

import java.io.*;
import java.net.*;

import javax.annotation.security.DeclareRoles;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author nithyasubramanian
 */
@DeclareRoles("javaee6user")
public class LoginServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET and POST methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String userName = request.getParameter("txtUserName");
            String password = request.getParameter("txtPassword");
            
            out.println("Before Login"+"<br>
"); out.println("IsUserInRole?.." + request.isUserInRole("javaee6user")+"<br>"); out.println("getRemoteUser?.." + request.getRemoteUser()+"<br>"); out.println("getUserPrincipal?.." + request.getUserPrincipal()+"<br>"); out.println("getAuthType?.." + request.getAuthType()+"<br>
"); try { request.login(userName, password); }catch(ServletException ex) { out.println("Login Failed with a ServletException.." + ex.getMessage()); return; } out.println("After Login..."+"<br>
"); out.println("IsUserInRole?.." + request.isUserInRole("javaee6user")+"<br>"); out.println("getRemoteUser?.." + request.getRemoteUser()+"<br>"); out.println("getUserPrincipal?.." + request.getUserPrincipal()+"<br>"); out.println("getAuthType?.." + request.getAuthType()+"<br>
"); request.logout(); out.println("After Logout..."+"<br>
"); out.println("IsUserInRole?.." + request.isUserInRole("javaee6user")+"<br>"); out.println("getRemoteUser?.." + request.getRemoteUser()+"<br>"); out.println("getUserPrincipal?.." + request.getUserPrincipal()+"<br>"); out.println("getAuthType?.." + request.getAuthType()+"<br>"); } finally { out.close(); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; } // </editor-fold> }

Other Java EE 6 examples (source code examples)

Here is a short list of links related to this Java EE 6 LoginServlet.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.