Getting servlet header

1328_project_explorer
index.jsp

Header.java

pom.xml

Result

1328_first

1328_second

Getting parameter values

Project Explorer :
1317_project_explorer

index.jsp

ParameterValues.java

pom.xml

Result
1317_first

1317_second

getting all request parameters in servlet

before this we have used request.getParameter(String) which is used to get a single request parameter value.
Now if we want to get all parameter names then we have to use request.getParameterNames()
it returns Enumeration of String objects of all parameter names.
After getting all parameter names we can get its value using request.getParameter(String) .
request.getParameterNames() is useful we have large number of parameters are used in request.
Following example is used to explain the above concepts.

Project Explorer:
1309_project_explorer

index.jsp

AllSerlvet.java

pom.xml

Result:
1309_first

1309_seconf

Servlet Maven Configuration Example

Maven is the Project Management Tool, that help developers to built the project easily (make create, add, update, compile, testing and deploy work easy).

  You can download Maven from Official Site

You can find servlet dependency file here

Here we will start a new project with maven

Project Explorer

Maven Project Structure

index.jsp

This Jsp page contains a text box and submit button.

On click on submit form will submit to PersonServlet action.

PersonServlet.java

pom.xml

Result

Servlet Maven example
Servlet Maven Response Page

Request dispatcher forwarding request

For login example we are using user mail id and password. if both are correct then we are forwaring to home.jsp page if any on of mail id or password is in correct then it will forwarded back to index.jsp page with appropriate error message.

1258_project_expo

index.jsp

Login.java

home.jsp

Result

1258_first

If user id and/or password is incorrect then it will be redirected to index.jsp page with appropriate error message.

1258_second

Using correct mailid and password [email protected] and 123456

1258_third

Submitting data to servlet using http post

Data sent using http post method is not visible at browser address bar

1249_project_explorer

index.jsp

Register.java

1249_first

1249_second

Sending data to servlet using http get method

Gererally http get is used to get information from server, but for getting specific information based on id or user name or based on query string we send id,user name or search term query to server.
Here we have created a form tag and two text box to take input age and name from user.
<form action=”SubmitServlet”> is sending data to SubmitServlet.
By default form method is get so it will call doGet() of SubmitServlet.
<form action=”SubmitServlet”> is equivalent to <form action=”SubmitServlet” method=”get”>
While using http get method the submitted content(name and age) is displaying on address bar.

3_project_explor

index.jsp

SubmitServlet.java

Result:
Address bar shows http://localhost:8084/ServletTutorial2/SubmitServlet?name=Ram+Kumar&age=22
after ? it is showing submitted data.

3_first

3_second

Servlet url and class mapping using web.xml

We can map url pattern with servlet class at web.xml.

Following example will illustrate this.

Project directory structure is as below:

2_explorer

index.jsp

web.xml

Welcome1.java

In web.xml url pattern(welcome1) will match to a logical name(welcome) that logical name is matched with servlet fully qualified class name(ebhor.servlet.Welcome1).

Result:
2_first

2_second

Servlet Annotation WebServlet Example

First example to show Servlet Welcome. For this we have created index.html page on index.html we have created a link. on click at link it will call welcome servlet.

Our project directory structure is as below

1_expo

index.jsp

Welcome.java

@WebServlet(name = “Welcome”, urlPatterns = {“/welcome”}) here urlPattern maps /welcome to Welcome.java.
<a href=”welcome”>Show welcome servlet</a> call to doGet() method of Welcome.java because anchor tag calls http get method so it is handled by servlets doGet() method.

Result
1_first

1_second