SlideShare a Scribd company logo
Abani: Programmatic login with Servlet 3.0


                                       Share     Report Abuse       Next Blog»                                                           Create Blog          Sign In




          Abani
          WEDNESDAY, MAY 2, 2012




          Programmatic login with Servlet 3.0
          In this blog I will describe you how we can achieve programmatic login with Servlet 3.0 compliant
          JEE server or container. 



          The HttpServletRequest object in Servlet3 has got few new methods.  Few of them are listed
          below.
                 login(username, password) - validate the user with specified username & password
                 with the default realm & creates an authenticated subject in the current request.            SEARCH THIS BLOG
                 logout() - logs out the current logged in user/subject
                                                                                                                                                     Search

                 getParts() - returns array of Multi-part objects available in the request.
                 getPart(field) - return the multi-part object associated with the field name
                                                                                                              ABANI
          In our case, I will use login & logout methods respectively.

          The software used to illustrate the programmatic login are listed below.
                 NetBeans 7.1.1 ( IDE )
                 Tomcat 7.0.27 ( Server / Container )
                 Ubuntu LTS 12.04 ( OS )
          I have created a dynamic web application named LoginDemo & my target runtime server is
          tomcat7. The project structure looks like below.


                                                                                                              BLOG ARCHIVE

                                                                                                              ▼  2012 (3)
                                                                                                                ▼  May (2)
                                                                                                                   Programmatic login with Servlet 3.0
                                                                                                                   Accessing EJB methods from JSF pages
                                                                                                                     via CDI

                                                                                                                ►  February (1)

                                                                                                              ►  2011 (10)
                                                                                                              ►  2010 (2)
                                                                                                              ►  2009 (2)




          below. I have created two jsp pages index.jsp & myaccount.jsp. index.jsp has the login form
          where I will submit my action to a servlet, where I will perform my authentication.



          To authenticate the user inputs I have created a servlet named AuthServlet in my application. The
          AuthServlet holds the business logic to perform login. Please see the code snippet below. 



           private void processLogin(HttpServletRequest request, HttpServletResponse



http://abani-behera.blogspot.in/2012/05/programmatic-login-in-servlet-30.html[03-05-2012 11:54:55]
Abani: Programmatic login with Servlet 3.0

           response) throws IOException, ServletException {
                   String username = request.getParameter("uid");
                   String password = request.getParameter("pwd");
                   RequestDispatcher rd = null;
                   try {
                       request.login(username, password);
                       rd = request.getRequestDispatcher("myaccount.jsp");
                      
                   } catch(Exception e){
                       request.setAttribute("msg", "Unable to login with " + username +
           "");
                       rd = request.getRequestDispatcher("login.jsp");
                   }
                   rd.forward(request, response);
               }

          The request object has login() method. which takes 2 parameters. The first one is the username &
          second parameter as password. Then the login method validates the username/password with the
          default realm. In case of tomcat the default realm is configured with file based tomcat-users.xml
          file. which can be found under $CATALINA_HOME/conf directory.



          So we have to configure few users in the tomcat-users.xml file. See below for a sample config.




          Tomcat creates an authenticated subjected by matching the username/password provided with the
          username password available in tomcat-users.xml file (The file based realm is the default realm in
          tomcat, we can change it to LDAPRealm/JNDIRealm, JDBCRealm, JAASRealm) .



          Similar to login() method, the request object has another method named logout(), which just logs
          out the user from current authenticated subject. See the code below for logout functionality.




           protected void processRequest(HttpServletRequest request, HttpServletResponse
           response)
                       throws ServletException, IOException {
                   if ( request.getServletPath().equalsIgnoreCase("/login") ) {
                       processLogin(request, response);
                   }
                   else {
                       request.logout();
                       request.setAttribute("msg", "You have successfully logged out of
           system");
                       request.getRequestDispatcher("index.jsp").forward(request,
           response);
                   }
               }



          The complete code can be checked in at Google svn & can be found here. The screen shots  of the
          above application in running mode are given below.




http://abani-behera.blogspot.in/2012/05/programmatic-login-in-servlet-30.html[03-05-2012 11:54:55]
Abani: Programmatic login with Servlet 3.0




          Let me know if you find any difficulty in accessing or executing the code.



          Thanks,
          Abani R. Behera


            Posted by abani behera at 4:17 AM




          0 comments:


          Post a Comment




http://abani-behera.blogspot.in/2012/05/programmatic-login-in-servlet-30.html[03-05-2012 11:54:55]
Abani: Programmatic login with Servlet 3.0




          Comment as:          Select profile...
                               Select profile...


              Publish    Preview




                                                           Home                                              Older Post

          Subscribe to: Post Comments (Atom)




                                                                Simple template. Template images by gaffera. Powered by Blogger.




http://abani-behera.blogspot.in/2012/05/programmatic-login-in-servlet-30.html[03-05-2012 11:54:55]

More Related Content

PDF
Java Programming - 08 java threading
PPTX
Introduction to JPA (JPA version 2.0)
PPTX
Poject documentation deepak
PPS
Ado.net session13
PDF
SQL injection exploitation internals
PPTX
Parsing in ios to create an app
PDF
Rich Internet Applications con JavaFX e NetBeans
PPT
2. oop with c++ get 410 day 2
Java Programming - 08 java threading
Introduction to JPA (JPA version 2.0)
Poject documentation deepak
Ado.net session13
SQL injection exploitation internals
Parsing in ios to create an app
Rich Internet Applications con JavaFX e NetBeans
2. oop with c++ get 410 day 2

What's hot (12)

PPT
Entity Persistence with JPA
PPTX
Entity Framework: Nakov @ BFU Hackhaton 2015
PPTX
java drag and drop and data transfer
PPTX
Spring JDBCTemplate
PDF
Introduction to JPA and Hibernate including examples
PDF
Java persistence api 2.1
ODP
Mahoodle (English)
PPTX
Introduction to JPA Framework
PPTX
Ajax
PDF
OpenDMS - the first 2 weeks
PDF
Everything about Object Oriented Programming
DOCX
Cis 407 i lab 6 of 7
Entity Persistence with JPA
Entity Framework: Nakov @ BFU Hackhaton 2015
java drag and drop and data transfer
Spring JDBCTemplate
Introduction to JPA and Hibernate including examples
Java persistence api 2.1
Mahoodle (English)
Introduction to JPA Framework
Ajax
OpenDMS - the first 2 weeks
Everything about Object Oriented Programming
Cis 407 i lab 6 of 7
Ad

Viewers also liked (18)

PDF
Summer of Tech 2015: Scrum masterclass
PDF
Web of things
ODP
Semantic Web And Coldfusion
PDF
TAMZ 2010 YEAR IN REVIEW
PDF
Can MVP help Government innovate like a Startup?
PDF
Advanced Replication Internals
PDF
What is your PaaS
PDF
Docker build, test and deploy saa s applications
PDF
TAMZ 2009 YEAR IN REVIEW
PDF
MongoDB Datacenter Awareness (mongosf2012)
PDF
Distribuzioni di Probabilita e Variabili Casuali
PDF
Harnessing The Semantic Web
PDF
Grails And The Semantic Web
PDF
Lean System Design Presentation from AgileNZ 2012
PDF
Lean Principles
PDF
#8 agile governance questions you can and should be asking
PPTX
MongoDB: tips, trick and hacks
PPTX
MongoDB: Easy Java Persistence with Morphia
Summer of Tech 2015: Scrum masterclass
Web of things
Semantic Web And Coldfusion
TAMZ 2010 YEAR IN REVIEW
Can MVP help Government innovate like a Startup?
Advanced Replication Internals
What is your PaaS
Docker build, test and deploy saa s applications
TAMZ 2009 YEAR IN REVIEW
MongoDB Datacenter Awareness (mongosf2012)
Distribuzioni di Probabilita e Variabili Casuali
Harnessing The Semantic Web
Grails And The Semantic Web
Lean System Design Presentation from AgileNZ 2012
Lean Principles
#8 agile governance questions you can and should be asking
MongoDB: tips, trick and hacks
MongoDB: Easy Java Persistence with Morphia
Ad

Similar to Programmatic login with servlet 3.0 (20)

PPTX
EXAMPPTGPW.pptx
PPTX
Request dispacther interface ppt
DOCX
Server side programming bt0083
PPTX
Sessionex1
PPTX
CS8651 IP Unit 3.pptx
PDF
Bt0083 server side programing 2
PDF
Servlets
PPTX
Utilize the Full Power of GlassFish Server and Java EE Security
PPTX
Java web application development
PPTX
Web Technologies - forms and actions
PPT
session and cookies.ppt
PPTX
SCWCD : Session management : CHAP : 6
PPTX
J2ee servlet
PDF
005428052.pdf
PPTX
UNIT-3 Servlet
PPTX
java Servlet technology
PPTX
servlets sessions and cookies, jdbc connectivity
PPTX
Advance java session 19
DOCX
Introduction to servlet
EXAMPPTGPW.pptx
Request dispacther interface ppt
Server side programming bt0083
Sessionex1
CS8651 IP Unit 3.pptx
Bt0083 server side programing 2
Servlets
Utilize the Full Power of GlassFish Server and Java EE Security
Java web application development
Web Technologies - forms and actions
session and cookies.ppt
SCWCD : Session management : CHAP : 6
J2ee servlet
005428052.pdf
UNIT-3 Servlet
java Servlet technology
servlets sessions and cookies, jdbc connectivity
Advance java session 19
Introduction to servlet

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Institutional Correction lecture only . . .
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharma ospi slides which help in ospi learning
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Classroom Observation Tools for Teachers
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Presentation on HIE in infants and its manifestations
Microbial disease of the cardiovascular and lymphatic systems
GDM (1) (1).pptx small presentation for students
Institutional Correction lecture only . . .
human mycosis Human fungal infections are called human mycosis..pptx
Anesthesia in Laparoscopic Surgery in India
Pharma ospi slides which help in ospi learning
O5-L3 Freight Transport Ops (International) V1.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Supply Chain Operations Speaking Notes -ICLT Program
Classroom Observation Tools for Teachers
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
A systematic review of self-coping strategies used by university students to ...
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Presentation on HIE in infants and its manifestations

Programmatic login with servlet 3.0

  • 1. Abani: Programmatic login with Servlet 3.0 Share Report Abuse Next Blog» Create Blog Sign In Abani WEDNESDAY, MAY 2, 2012 Programmatic login with Servlet 3.0 In this blog I will describe you how we can achieve programmatic login with Servlet 3.0 compliant JEE server or container.  The HttpServletRequest object in Servlet3 has got few new methods.  Few of them are listed below. login(username, password) - validate the user with specified username & password with the default realm & creates an authenticated subject in the current request. SEARCH THIS BLOG logout() - logs out the current logged in user/subject Search getParts() - returns array of Multi-part objects available in the request. getPart(field) - return the multi-part object associated with the field name ABANI In our case, I will use login & logout methods respectively. The software used to illustrate the programmatic login are listed below. NetBeans 7.1.1 ( IDE ) Tomcat 7.0.27 ( Server / Container ) Ubuntu LTS 12.04 ( OS ) I have created a dynamic web application named LoginDemo & my target runtime server is tomcat7. The project structure looks like below. BLOG ARCHIVE ▼  2012 (3) ▼  May (2) Programmatic login with Servlet 3.0 Accessing EJB methods from JSF pages via CDI ►  February (1) ►  2011 (10) ►  2010 (2) ►  2009 (2) below. I have created two jsp pages index.jsp & myaccount.jsp. index.jsp has the login form where I will submit my action to a servlet, where I will perform my authentication. To authenticate the user inputs I have created a servlet named AuthServlet in my application. The AuthServlet holds the business logic to perform login. Please see the code snippet below.  private void processLogin(HttpServletRequest request, HttpServletResponse http://abani-behera.blogspot.in/2012/05/programmatic-login-in-servlet-30.html[03-05-2012 11:54:55]
  • 2. Abani: Programmatic login with Servlet 3.0 response) throws IOException, ServletException {         String username = request.getParameter("uid");         String password = request.getParameter("pwd");         RequestDispatcher rd = null;         try {             request.login(username, password);             rd = request.getRequestDispatcher("myaccount.jsp");                     } catch(Exception e){             request.setAttribute("msg", "Unable to login with " + username + "");             rd = request.getRequestDispatcher("login.jsp");         }         rd.forward(request, response);     } The request object has login() method. which takes 2 parameters. The first one is the username & second parameter as password. Then the login method validates the username/password with the default realm. In case of tomcat the default realm is configured with file based tomcat-users.xml file. which can be found under $CATALINA_HOME/conf directory. So we have to configure few users in the tomcat-users.xml file. See below for a sample config. Tomcat creates an authenticated subjected by matching the username/password provided with the username password available in tomcat-users.xml file (The file based realm is the default realm in tomcat, we can change it to LDAPRealm/JNDIRealm, JDBCRealm, JAASRealm) . Similar to login() method, the request object has another method named logout(), which just logs out the user from current authenticated subject. See the code below for logout functionality. protected void processRequest(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {         if ( request.getServletPath().equalsIgnoreCase("/login") ) {             processLogin(request, response);         }         else {             request.logout();             request.setAttribute("msg", "You have successfully logged out of system");             request.getRequestDispatcher("index.jsp").forward(request, response);         }     } The complete code can be checked in at Google svn & can be found here. The screen shots  of the above application in running mode are given below. http://abani-behera.blogspot.in/2012/05/programmatic-login-in-servlet-30.html[03-05-2012 11:54:55]
  • 3. Abani: Programmatic login with Servlet 3.0 Let me know if you find any difficulty in accessing or executing the code. Thanks, Abani R. Behera Posted by abani behera at 4:17 AM 0 comments: Post a Comment http://abani-behera.blogspot.in/2012/05/programmatic-login-in-servlet-30.html[03-05-2012 11:54:55]
  • 4. Abani: Programmatic login with Servlet 3.0 Comment as: Select profile... Select profile... Publish Preview Home Older Post Subscribe to: Post Comments (Atom) Simple template. Template images by gaffera. Powered by Blogger. http://abani-behera.blogspot.in/2012/05/programmatic-login-in-servlet-30.html[03-05-2012 11:54:55]