Saturday, September 26, 2009

Chapter 10: More Examples: JSP + Servlets



What now?

Both JSP and servlets combined together forms a better web application. Lets write a sample application that involves both JSP and Servlets.
we will use four files for this example.
  1. home.jsp
  2. AuthenticateServlet.java
  3. welcome.jsp
  4. error.jsp

Examples

index.jsp

This is like our login page where the user will enter an username and password.
<HTML>
<BODY>     
<h5>Welcome to VikiMail !</h5>      
<h4>Login</h4>      
<FORM ACTION = "AuthenticateUser" METHOD ="POST">      
Username: <INPUT TYPE = "TEXT" NAME = "username">      
Password: <INPUT TYPE = "PASSWORD" NAME = "password">      
<INPUT TYPE = "SUBMIT" VALUE = "Login">      
</FORM>
</BODY>     
</HTML>

AuthenticateServlet.java

This is the only servlet program in our example. It will be invoked when we enter username/password and submit the index.jsp page.
For making it simple, this servlet just checks if the password is super*, goes to success.jsp. Otherwise it will go to error.jsp
import java.io.*;    
import javax.servlet.*;     
import javax.servlet.http.*; 
public class AuthenticateServlet extends HttpServlet     
{      
public void doPost     
(HttpServletRequest request,      
HttpServletResponse response)     
throws ServletException, IOException      
{      
String username = request.getParameter("username").toString();    
String password = request.getParameter("password").toString();
if(password.equals("super*"))     
response.sendRedirect("success.jsp");      
else      
response.sendRedirect("error.jsp");     
}     
} 

web.xml

This file, usually resides inside the WEB-INF folder. Whatever servlets we have written in our application, we need to specify it here.
In our case there is one servlet called AuthenticateServlet.java.

Mapping with web.xml

Follow these steps to add a servlet mapping.
1) Create a <servlet> </servlet> tag.
2) Create a <servlet-name> tag and <servlet-class> tag INSIDE the <servlet> tag like shown below.
<servlet>  
<servlet-name> </servlet-name>    
<servlet-class>  </servlet-class>    
</servlet> 
3) In <servlet-class>, give the .class name of the servlet program. The name you specify here must be the same name as the servlet program file name.
4) In </servlet-name> tag, give any dummy name. This is like a reference name for mapping purposes. The dummy name given here needs be specified exactly in another place of the web.xml file.

<servlet>   
<servlet-name>CallAuthenticate</servlet-name>   
<servlet-class>AuthenticateServlet</servlet-class>    
</servlet> 
5) Create a <servlet-mapping> tag and inside that, create two other tags namely, <servlet-name> and <url-pattern> like the one which is shown below.
<servlet-mapping>  
<servlet-name>   </servlet-name>    
<url-pattern>       </url-pattern>    
</servlet-mapping> 
6) In the <servlet-name> tag (under <servlet-mapping>), give the same dummy name that you specified in the <servlet-name> tag under <servlet> tag.
7) Finally in the <url-pattern> tag, give this symbol first. : /
8) Followed by the slash, give any URL name.
Remember: This URL pattern will be specified in the ACTION attribute of the <form> tag in the jsp page. (Check the index.jsp page)
After changing everything, your web.xml will look something like this.
Note: Follow the same procedure for creating servlet mappings. Except for the (1)Actual java file name (2) dummy servlet name and the (3) URL Pattern, everything procedure remains same.

web.xml

<?xml version="1.0"?>  
<web-app    
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 
<display-name>Demo Application</display-name>  
<description>   
For Learning purposes.    
</description>
<servlet>  
<servlet-name>CallAuthenticate</servlet-name>   
<servlet-class>AuthenticateServlet</servlet-class>   
</servlet>
<servlet-mapping>   
<servlet-name>CallAuthenticate</servlet-name>   
<url-pattern>/AuthenticateUser</url-pattern>   
</servlet-mapping>
</web-app>

success.jsp

This is the page that the servlet re-directs to when the password is correct.
<HTML>     
<h5>Welcome!</h5><BR>    
<h4>Login Successful</h4>     
</HTML>

error.jsp

This is the page that the servlet re-directs to when the password is wrong.
<HTML>     
<h5>Sorry!</h5><BR>     
<h4>Login failed</h4>      
</HTML>

12 comments:

  1. good work man.... all the best.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. How to communicate java socket application using JSP for web apps?

    ReplyDelete
  4. @Brad: Interesting question you have asked there. Java is a wonderful language which almost provided anything a programmer wanted.

    It gave options to write and read from Sockets.
    It gave options to write console based apps.
    It gave options to build GUI based Desktop applications using AWT and Applet..
    It then gave this J2EE extension which enabled us to write client server programs and web-based applications.

    One interesting feature among all these is that we can put a socket program straight into a JSP.

    JSP can run ANY Java program.
    A Java program can communicate with Sockets.
    So, JSP can communicate to sockets.


    Note: Although using JSP to connect to sockets is possible, it's not the right practice. JSP are supposed to be used to PRESENT CONTENT to the USER. To do this Socket commmunication and things, you can use Servlets, which are meant exactly for this purpose: Back-end operations.

    ReplyDelete
  5. How to learn jsp, jdbc, servlets for an interview in 2 days? New to java but want to learn above technologies?

    ReplyDelete
  6. @Fico:
    This is like reading and understanding The Bible in 2 days, which is not possible. I suggest You try and better your understanding of the core concepts by extensively reading random internet articles on these topics!
    -Vignesh

    ReplyDelete
  7. Terrific example here. I found it very straightforward, especially compared to everything else out there. The accompanying video is very helpful as well. Thanks much to the author(s)!

    ReplyDelete
  8. How to log out of a session in jsp and how to make subsequent back clicks be redirected to my login page?

    ReplyDelete
  9. Is Using JSP pages with Oracle databse a good approach to develop well structed and secure web applications?

    ReplyDelete
  10. Thanks for such an informative blog post, you helped me a lot in learning many important aspects. keep guiding.

    ReplyDelete
  11. My apache server didn't start,how to rectify this problem? pls help sir.

    ReplyDelete
  12. How to compare the 2 user input strings in jsp sir......

    ReplyDelete

Our Google Group

Our Facebook Group