Create a dynamic web project
For that go to
FileànewàDynamic web project
Fill project details and say next .next and finish and make sure generate web.xml
is checked
Now time to create a servlet
For that right click on create project “SeverletJSPExapmle” ànewèservlet and give entries
Now in next we need to edit the URL mapping to make it more
convenient because we are going to use this to access our servlet so I made
them all small letter
Now say next and next and check the option . Here we removed the dopost and doget and selected services.
As they do same job
And say finish
Now eclipse has created
ServletExample.java file for us in source directory and added following
entry in web.xml (WebContentàWEB-INFàweb.xml)
<servlet>
<description></description>
<display-name>ServletExample</display-name>
<servlet-name>ServletExample</servlet-name>
<servlet-class>com.example.tutorial.ServletExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletExample</servlet-name>
<url-pattern>/servletexample</url-pattern>
</servlet-mapping>
Now we will do some coding to interact our servlet with web
browser
protected void
service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// it will get
object to write string in browser
PrintWriter
out = response.getWriter();
out.println("hello
Java!!!");
}
Now run this code by doing right click in ServletExample.Java
file and select run AsàRun On Server
You will see your string in URL
You might need to restart once that tomcat server
Now time to some more advance programming by capturing
request from JSP (xhtml) pages and sending response back
To create jsp page browse to webcontent folder and select newà
Jsp page
Now give JSP page name say index.jsp and click next and in
next screen select “New JSP file
(xhtml)” as JSP template and say Finish
Now index.jsp page get created. To deploy the jsp page you
need to right click on page and say new->run as-> on application sever
Here we will create a jsp page having a form with 2 input
field firstname and lastname with a submit button
We will fill jsp form and capture that request in serverlet
and display in web browser
We already created index.jsp page now time to add some code
<body>
<form action="servletexample" method="post" >
<table border="0" >
<tr>
<td>First Name: </td> <td><input type="text" name="firstname"/></td>
</tr>
<tr>
<td>Last Name: </td> <td><input type="text" name ="lastname"/></td>
</tr>
<tr>
<td colspan="2"><input type = "submit" value = "Submit"/></td>
</tr>
</table>
</form>
Here <form> to is to create a form
action= <give
your servlet name (Not serverlet class name)> this is the place which setup
binding between servlet and jsp
method = post means user will post below information as
request from browse
<table> tag use to give good look and provide
formatting
<tr> tag is to separate rows in table
<td> tag is for column
<td colspan="2"> means we are combining two column into one
Earlier we added very simple out put string in our
ServletExample.java class here we are going to add some more code to capture
JSP from request and display them in browser
protected void service(HttpServletRequest
request, HttpServletResponse response) throws ServletException, IOException {
// Get servlet object which will display any
string in browser
PrintWriter out = response.getWriter();
// Capture request from JSP page where input field name ="firstname"
String fname = request.getParameter("firstname");
// Capture request from JSP page where input field name ="lastname"
String lname = request.getParameter("lastname");
// Display string in web browser
out.println(fname+" "+lname);
//out.println("hello Java!!!");
}
Now you can access form by URL: http://localhost:8081/ServerletsJSPExample/index.jsp
As JSP page should not be directly accessible to user so
we will use RequestDispatcher to hide
our jsp page.
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//it will hide the jsp page link in URL and just show
servlet URL it will use MVC model
//here we have add if condition so that jsp form will
show if any input is null
if(request.getParameter("firstname")==null
|| request.getParameter("lastname")==null ){
getServletContext().getRequestDispatcher("/index.jsp").forward(request,
response);
}
// Capture request from JSP page where input field name
="firstname"
String fname =
request.getParameter("firstname");
// Capture request from JSP page where input field name
="lastname"
String lname =
request.getParameter("lastname");
// Display string in web browser
out.println(fname+" "+lname);
}
Now you can see index.jsp page from URL:
http://localhost:8081/ServerletsJSPExample/ instead of
complete jsp page URL http://localhost:8081/ServerletsJSPExample/index.jsp which we used
earlier
Now
we will create one more jsp page output.jsp which
will capture input entered in earlier created page index.jsp . This
page will display the output in web. Again we will hind both
JSP pages from user using RequestDispatcher . Actually it will provide
MVC model where web page will be display as per input . Like if any input first
name or last name will be null then always index.jsp page will populate and
once both entries will be there then output.jsp page will display first name
and last name
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//it will hide the jsp page link in URL and just show
servlet URL it will use MVC model
//here we have add if condition so that jsp form will
show if any input is null
//we will create one more jsp page to show the output
called output.jsp
if(request.getParameter("firstname")==null ||
request.getParameter("lastname")==null ){
getServletContext().getRequestDispatcher("/index.jsp").forward(request,
response);
}
// Capture request from JSP page where input field name
="firstname"
String fname =
request.getParameter("firstname");
// Capture request from JSP page where input field name
="lastname"
String lname =
request.getParameter("lastname");
// Display string in web browser
out.println(fname+" "+lname);
//code to display output in output.jsp page
//Here we are setting attribute in request which is
output.jsp page is fetching
request.setAttribute("firstname", fname);
request.setAttribute("lastname", lname);
//Get output.jsp page using request dispatcher
getServletContext().getRequestDispatcher("/output.jsp").forward(request,
response);
}
Output.jsp:
<title>JSP Output</title>
</head>
<body>
<h1>Your First and
Last name is:</h1>
<%
String
ffname= (String)request.getAttribute("firstname");
String
llname = (String)request.getAttribute("lastname");
out.print(ffname+" "+llname);
%>
</body>
<h1>
tag we use for heading as it is in normal html
<%
%> this tag use to write java scriptlet
Here
you will notice we are getting both string from request.getAttribute which is
set by servlet code. So servlet java code is working as mediocre which pop up
and took input from index.jsp after that
set value to request object which is accessed by output.jsp and use in Java
script and again servlet java code invoke the output.jsp
Now
if you will say run as server from ServletExample.java then first index.jsp
page will display but you will notice URL is :
http://localhost:8081/ServerletsJSPExample/servletexample instead of http://localhost:8081/ServerletsJSPExample/index.jsp
You
will give first name and last name and again output.jsp page URL will be :
http://localhost:8081/ServerletsJSPExample/servletexample instead of http://localhost:8081/ServerletsJSPExample/output.jsp
It
is because of request dispatcher you can see MVC model here.
The
other way is capturing request in servlet by directly passing the value through
URL like:
Complete working code:
protected void
service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// it will get
object to write string in browser
PrintWriter
out = response.getWriter();
if(request.getParameter("firstname") == null ||
request.getParameter("lastname")==null)
{
getServletContext().getRequestDispatcher("/index.jsp").forward(request,
response);
return;
}
String
fname = request.getParameter("firstname");
String
lname = request.getParameter("lastname");
request.setAttribute("firstname", fname);
request.setAttribute(
"lastname",lname);
getServletContext().getRequestDispatcher("/output.jsp").forward(request,
response);
}
We can also use variable define in java scriptlet in HTML tag to do more
formatting and in our example we will print first name and lastname in HTML
table in output.jsp using below code in body
<body>
<h1>Your First and
Last name is:</h1>
<%
String
ffname= (String)request.getAttribute("firstname");
String
llname = (String)request.getAttribute("lastname");
%>
<table>
<tr>
<td>First Name:</td><td><%=ffname %></td>
</tr>
<tr>
<td>Last Name:</td><td><%= llname%></td>
</tr>
</table>
</body>
Now the output will look like
Aucun commentaire:
Enregistrer un commentaire