Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Saturday, 3 August 2013

Jsp-Servlet Annotations Hello World Example Project in Eclipse

In this particular blog we will discuss about servlet annotations and how to use servlet annotations to eliminate web.xml configuration. Our objective for today's discussion is to create a simple hello world application with jsp and servlet using Servlet API 3.0 annotations.

Servlet API 3.0 included a new package called 'javax.servlet.annotation' which provides annotations to be used to annotate a servlet class and this can eliminate  the use of web.xml configuration. Servlet provides a number of useful annotations to be used, servlet containers processes these annotation classes at runtime. Some of useful annotations and their usage is listed below.
@WebFilter
@WebInitParam
@WebListener
@WebServlet
@HandlesTypes
@HttpConstraint
@HttpMethodConstraint
@MultipartConfig
@ServletSecurity


Annotating servlet using @WebServlet annotation

@WebServlet is used to declare a java class as servlet, a class still requires to extend HttpServlet. Some examples of using @WebServlet annotation are listed below.

The simplest way of declaring a servlet is annotating it with @WebServlet  and providing request mapping details in double cotes as shown below:
@WebServlet("/simpleServlet")
public class TestServlet {
 // servlet code goes here
}

Additional details can be provided to a servlet using annotations as shown in example below :
@WebServlet(name = "Simple Servlet",
            description = "This is a simple servlet with annotations",
            urlPatterns = "/getServlet")
public class TestServlet {
 // servlet code goes here
}

A servlet can have multiple request mapping to be called on, see the examplebelow:
@WebServlet(
           urlPatterns = {"/getServlet", "/simpleServlet", "/coolServlet"}
           )
public class TestServlet {
 // servlet code goes here
}


Create jsp-servlet helloworld application using annotations

Now let is create a simple helloworld application with jsp-servlet using annotations.

Project Structure

Here is overall project structure for today's discussion, create a simple 'Dynamic Web Project' from Eclipse IDE and add all required servlet and jsp files to it as mentioned in rest part of this tutorial.


\src\com\beingjavaguys\servlets\SimpleServlet.java

package com.beingjavaguys.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "Simple Servlet", description = "This is a simple servlet with annotations", urlPatterns = "/getServlet")
public class SimpleServlet extends HttpServlet {

 @Override
 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  System.out.println("get request");
  response.sendRedirect("register.jsp");
 }

 @Override
 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  System.out.println("Name : " + request.getParameter("name"));
  System.out.println("Gender : " + request.getParameter("gender"));
  System.out.println("Email : " + request.getParameter("email"));
  System.out.println("Phone : " + request.getParameter("phone"));
  System.out.println("City : " + request.getParameter("city"));

  request.getRequestDispatcher("details.jsp").forward(request, response);

 }
}


\WebContent\index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Being Java Guys | Jsp Servlet Annotation</title>
</head>
<body>
 <center>

  <div style="color: teal; font-size: 30px">Being Java Guys | Jsp
   Servlet Annotation</div>
  <a href="getServlet">Click Here </a>
 </center>
</body>
</html>

\WebContent\register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Being Java Guys | Registration Form</title>
</head>
<body>
 <center>
  <div style="color: teal; font-size: 30px">Being Java Guys |
   Registration Form</div>
  <form method="post" action="getServlet">
   <table>
    <tr>
     <td>Name :</td>
     <td><input type="text" name="name" />
     </td>
    </tr>
    <tr>
     <td>Gender :</td>
     <td><input type="radio" name="gender" value="male">Male<br />
      <input type="radio" name="gender" value="female">Feale</td>
    </tr>
    <tr>
     <td>Phone :</td>
     <td><input type="text" name="phone" />
     </td>
    </tr>
    <tr>
     <td>Email :</td>
     <td><input type="text" name="email" />
     </td>
    </tr>
    <tr>
     <td>City :</td>
     <td><select name="city">
       <option value="delhi">Delhi</option>
       <option value="mumbai">Mumbai</option>
       <option value="kolkata">Kolkata</option>
       <option value="chennai">Chennai</option>
       <option value="noida">Noida</option>
     </select>
     </td>
    </tr>
    <tr>
     <td> </td>
     <td><input type="submit" value="Save">
     </td>
    </tr>
   </table>
  </form>
 </center>
</body>
</html>

\WebContent\details.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Being Java Guys | Details</title>
</head>
<body>
 <center>
  <div style="color: teal; font-size: 30px">Being Java Guys |
   Details</div>
   <table border="1">
    <tr>
     <td>Name :</td>
     <td><%=request.getParameter("name")%></td>
    </tr>
    <tr>
     <td>Gender :</td>
     <td><%=request.getParameter("gender")%></td>
    </tr>
    <tr>
     <td>Phone :</td>
     <td><%=request.getParameter("phone")%></td>
    </tr>
    <tr>
     <td>Email :</td>
     <td><%=request.getParameter("email")%></td>
    </tr>
    <tr>
     <td>City :</td>
     <td><%=request.getParameter("city")%></td>
    </tr>
   </table>
 </center>
</body>
</html>

Now run the project on server, if everything goes right you will get following screens.





In this particular blog we came across ‘How to create a Hibernate Project in Maven’, 'Insert table in hibernate', 'Retrieve data from db in hibernate', 'Update a row in hibernate' and 'Delete data in hibernate'. In upcoming blogs we will see more about 'jsp-servlet', Java and other opensource technologies.






Thanks for reading !
Being Java Guys Team

Download "Jsp-Servlet Annotations Hello World Example Project" from "SkyDrive"





3 comments:

  1. great tutorial, resolves my mapping problems jsp-servlet!

    ReplyDelete
  2. Good tutorial with simple explanation.

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft