Upload
idil10
View
248
Download
0
Embed Size (px)
Citation preview
8/8/2019 Servlet&Jsp Process
1/40
Table of Contents
1. Eclipse Web Tool Platform
1.1. Overview of Eclipse WTP
1.2. Additional Eclipse WTP resources
1.3. Dynamic Web Project2. Tomcat Installation
3. Installation of WTP
4. WTP Configuration
4.1. Server
5. Servlets
5.1. Overview
5.2. Project
5.3. Creating Data Access Object
5.4. Creating the Servlet
5.5. Run6. JavaServer Pages (JSPs)
6.1. Overview
6.2. Create Project
6.3. Create the Controller (servlet)
6.4. Create the Views (JSP)
6.5. Run it
7. Web Archive - How to create a war file from Eclipse
8. Thank you
9. Questions and Discussion
10. Links and Literature
10.1. Source Code
10.2. Web development resources
10.3. Other Resources
1. Eclipse Web Tool Platform
1.1. Overview of Eclipse WTP
In case you are new to Java web development you may want to get a quick overview of
webdevelopment with Java in the following short article: Introduction to Java
Webdevelopment .
Eclipse WTP provides tools for developing standard Java web applications and Java EE
applications. Eclipse WTP simplifies the creation of web artifacts and provides a runtime
environment in which these artifacts can be deployed, started and debugged. Typical
http://www.vogella.de/articles/EclipseWTP/article.html#overviewhttp://www.vogella.de/articles/EclipseWTP/article.html#overview_wtphttp://www.vogella.de/articles/EclipseWTP/article.html#overview_additionshttp://www.vogella.de/articles/EclipseWTP/article.html#webprojecthttp://www.vogella.de/articles/EclipseWTP/article.html#installation_tomcathttp://www.vogella.de/articles/EclipseWTP/article.html#installationhttp://www.vogella.de/articles/EclipseWTP/article.html#configurationhttp://www.vogella.de/articles/EclipseWTP/article.html#serverhandlinghttp://www.vogella.de/articles/EclipseWTP/article.html#servletshttp://www.vogella.de/articles/EclipseWTP/article.html#servlets_overviewhttp://www.vogella.de/articles/EclipseWTP/article.html#servlets_projecthttp://www.vogella.de/articles/EclipseWTP/article.html#servlets_daohttp://www.vogella.de/articles/EclipseWTP/article.html#servlets_createservlethttp://www.vogella.de/articles/EclipseWTP/article.html#servlets_runservlethttp://www.vogella.de/articles/EclipseWTP/article.html#javaserverpageshttp://www.vogella.de/articles/EclipseWTP/article.html#javaserverpages_overviewhttp://www.vogella.de/articles/EclipseWTP/article.html#javaserverpages_projecthttp://www.vogella.de/articles/EclipseWTP/article.html#javaserverpages_controllerhttp://www.vogella.de/articles/EclipseWTP/article.html#javaserverpages_viewhttp://www.vogella.de/articles/EclipseWTP/article.html#javaserverpages_runhttp://www.vogella.de/articles/EclipseWTP/article.html#wtp_warhttp://www.vogella.de/articles/EclipseWTP/article.html#thankyouhttp://www.vogella.de/articles/EclipseWTP/article.html#questionshttp://www.vogella.de/articles/EclipseWTP/article.html#resourceshttp://www.vogella.de/articles/EclipseWTP/article.html#sourcecodehttp://www.vogella.de/articles/EclipseWTP/article.html#resources_s1http://www.vogella.de/articles/EclipseWTP/article.html#resources_generalhttp://www.vogella.de/articles/JavaWebTerminology/article.htmlhttp://www.vogella.de/articles/JavaWebTerminology/article.htmlhttp://www.vogella.de/articles/EclipseWTP/article.html#overview_wtphttp://www.vogella.de/articles/EclipseWTP/article.html#overview_additionshttp://www.vogella.de/articles/EclipseWTP/article.html#webprojecthttp://www.vogella.de/articles/EclipseWTP/article.html#installation_tomcathttp://www.vogella.de/articles/EclipseWTP/article.html#installationhttp://www.vogella.de/articles/EclipseWTP/article.html#configurationhttp://www.vogella.de/articles/EclipseWTP/article.html#serverhandlinghttp://www.vogella.de/articles/EclipseWTP/article.html#servletshttp://www.vogella.de/articles/EclipseWTP/article.html#servlets_overviewhttp://www.vogella.de/articles/EclipseWTP/article.html#servlets_projecthttp://www.vogella.de/articles/EclipseWTP/article.html#servlets_daohttp://www.vogella.de/articles/EclipseWTP/article.html#servlets_createservlethttp://www.vogella.de/articles/EclipseWTP/article.html#servlets_runservlethttp://www.vogella.de/articles/EclipseWTP/article.html#javaserverpageshttp://www.vogella.de/articles/EclipseWTP/article.html#javaserverpages_overviewhttp://www.vogella.de/articles/EclipseWTP/article.html#javaserverpages_projecthttp://www.vogella.de/articles/EclipseWTP/article.html#javaserverpages_controllerhttp://www.vogella.de/articles/EclipseWTP/article.html#javaserverpages_viewhttp://www.vogella.de/articles/EclipseWTP/article.html#javaserverpages_runhttp://www.vogella.de/articles/EclipseWTP/article.html#wtp_warhttp://www.vogella.de/articles/EclipseWTP/article.html#thankyouhttp://www.vogella.de/articles/EclipseWTP/article.html#questionshttp://www.vogella.de/articles/EclipseWTP/article.html#resourceshttp://www.vogella.de/articles/EclipseWTP/article.html#sourcecodehttp://www.vogella.de/articles/EclipseWTP/article.html#resources_s1http://www.vogella.de/articles/EclipseWTP/article.html#resources_generalhttp://www.vogella.de/articles/JavaWebTerminology/article.htmlhttp://www.vogella.de/articles/JavaWebTerminology/article.htmlhttp://www.vogella.de/articles/EclipseWTP/article.html#overview8/8/2019 Servlet&Jsp Process
2/40
web artifacts in a Java environment are HTML pages, XML files, webservices, servlets
and JSPs.
The following article will focus on the creation of servlets and JSP with Eclipse WTP and
how to use the runtime environment of Eclipse to test them.
Eclipse WTP supports all mayor webcontainer, e.g. Jetty and Apache Tomcat as well as
the mayor Java EE application server. This tutorial will be based on Apache Tomcat.
1.2. Additional Eclipse WTP resources
The development of webservices with Eclipse WTP is covered in Webservices with
Axis2 and the Eclipse Web Tool Platform (WTP) - Tutorial .
The development of JavaServerFaces is covered in JavaServer Faces (JSF)
development with Eclipse WTP JSF - Tutorial and JSF with Apache Myfaces Trinidad
and Eclipse .
1.3. Dynamic Web Project
Eclipse uses builders which are responsible for working with the relevant artifacts.
Eclipse WTP uses "Dynamic Web Projects". These projects provide the necessary
builders to run, debug and deploy a Java web application. Therefore for the
development of Java web application you create "Dynamic Web Projects" .
2. Tomcat Installation
Download the Tomcat server 6.0.x from the following webpage
http://tomcat.apache.org/
Installing Tomcat (on windows) is very easy and self explaining as it comes with a
standard installer. For an overview of the usage and configuration of Tomcat see
Apache Tomcat - Tutorial . For a tomcat installation on other platform please use
Google.
After the installation test if Tomcat in correctly installed by opening a browser to
http://localhost:8080/.
This should open the Tomcat main page. If it works then Tomcat is correctly installed.
http://www.vogella.de/articles/Webservice/article.htmlhttp://www.vogella.de/articles/Webservice/article.htmlhttp://www.vogella.de/articles/JavaServerFaces/article.htmlhttp://www.vogella.de/articles/JavaServerFaces/article.htmlhttp://www.vogella.de/articles/ApacheMyFaces/article.htmlhttp://www.vogella.de/articles/ApacheMyFaces/article.htmlhttp://tomcat.apache.org/http://www.vogella.de/articles/ApacheTomcat/article.htmlhttp://www.vogella.de/articles/Webservice/article.htmlhttp://www.vogella.de/articles/Webservice/article.htmlhttp://www.vogella.de/articles/JavaServerFaces/article.htmlhttp://www.vogella.de/articles/JavaServerFaces/article.htmlhttp://www.vogella.de/articles/ApacheMyFaces/article.htmlhttp://www.vogella.de/articles/ApacheMyFaces/article.htmlhttp://tomcat.apache.org/http://www.vogella.de/articles/ApacheTomcat/article.html8/8/2019 Servlet&Jsp Process
3/40
Stop now Tomcat before starting Eclipse. If Tomcat is still running Eclipse WTP will
have problems using the installed Tomcat.
3. Installation of WTP
Depending on your Eclipse installation you also need to install the WTP tools. Use the
update manager to install the all packages from "Web, XML, and Java EE
Development" except "PHP Development" and "Eclipse RAP" from the standard Galileo
site. Please see Using the Eclipse Update Manager
http://www.vogella.de/articles/Eclipse/article.html#updatemanagerhttp://www.vogella.de/articles/Eclipse/article.html#updatemanager8/8/2019 Servlet&Jsp Process
4/40
4. WTP Configuration
You have to configure WTP to use the Tomcat installation.
Select Windows -> Preferences -> Server -> Runtime Environments. Press Add.
Select your version of Tomcat.
8/8/2019 Servlet&Jsp Process
5/40
To compile the JSP into servlets you need to use the JDK. In my case the "Workbench
default JRE" is pointing to the JDK. You can check you setup by clicking on "Installed
JRE".
8/8/2019 Servlet&Jsp Process
6/40
Press Finish and then Ok. You are now ready to use Tomcat with WTP.
4.1. Server
During development you will create your server. You can manager you server via the
server view. To see if you data was persisted stop and restart the server. You can do
this via the Windows -> Show View -> Servers -> Servers
8/8/2019 Servlet&Jsp Process
7/40
The following shows where you can start, stop and restart your server.
5. Servlets
8/8/2019 Servlet&Jsp Process
8/40
5.1. Overview
We will create a servlet which works as a webpage counter. This servlet will keep track
of the number of visitors of a webpage. The servlet will persists the number of visitors in
a text file.
5.2. Project
Create a new dynamic web project called "de.vogella.wtp.filecounter" by selecting File
-> New -> Other -> Web -> Dynamic Web Project.
http://www.vogella.de/articles/JavaWebTerminology/article.html#servlethttp://www.vogella.de/articles/JavaWebTerminology/article.html#servlet8/8/2019 Servlet&Jsp Process
9/40
Press finished. If asked if you want to switch to the Java EE Perspective answer yes.
A new project has been created with the standard structure of a Java web application.The WEB-INF/lib directory will later hold all the JAR files that the Java web application
requires.
5.3. Creating Data Access Object
8/8/2019 Servlet&Jsp Process
10/40
Create a new package "de.vogella.wtp.filecounter.dao" .
Create the Java class which will provide the number of visitors write this value to a file.
package de.vogella.wtp.filecounter.dao;
import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;
public class FileDao {
public int getCount() {int count = 0;// Load the file with the counterFileReader fileReader = null;BufferedReader bufferedReader = null;PrintWriter writer = null ;try {
File f = new File("FileCounter.initial");if (!f.exists()) {
f.createNewFile();writer = new PrintWriter(new FileWriter(f));writer.println(0);
}if (writer !=null){
writer.close();}
fileReader = new FileReader(f);bufferedReader = new BufferedReader(fileReader);String initial = bufferedReader.readLine();count = Integer.parseInt(initial);
} catch (Exception ex) {if (writer !=null){
writer.close();}
}if (bufferedReader != null) {
try {bufferedReader.close();
} catch (IOException e) {e.printStackTrace();
}}return count;
}
public void save(int count) throws Exception {
FileWriter fileWriter = null;PrintWriter printWriter = null;fileWriter = new FileWriter("FileCounter.initial");printWriter = new PrintWriter(fileWriter);printWriter.println(count);
// Make sure to close the fileif (printWriter != null) {
printWriter.close();}
8/8/2019 Servlet&Jsp Process
11/40
}
}
This Java class is not a servlet, it is a normal Java class.
5.4. Creating the Servlet
Create a servlet. Right click on the folder Webcontent and select New-> Other. Select
Web -> Servlet. Maintain the following data.
Press finish.
8/8/2019 Servlet&Jsp Process
12/40
You could also create a servlet without the wizard. The wizard creates a Java class
which extends javax.servlet.http.HpptServlet and add the servlet settings to the web.xml
description file.
Maintain the following code for the servlet.
package de.vogella.wtp.filecounter.servlets;
import java.io.IOException;import java.io.PrintWriter;
import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;
import de.vogella.wtp.filecounter.dao.FileDao;
/*** Servlet implementation class FileCounter*/public class FileCounter extends HttpServlet {
private static final long serialVersionUID = 1L;
int count;private FileDao dao;
public void init() throws ServletException {dao = new FileDao();try {
count = dao.getCount();} catch (Exception e) {
getServletContext().log("An exception occurred in FileCounter",e);
throw new ServletException("An exception occurred inFileCounter"+ e.getMessage());
}}
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,
IOException {// Set a cookie for the user, so that the counter does not increate// everytime the user press refreshHttpSession session = request.getSession(true);// Set the session valid for 5 secssession.setMaxInactiveInterval(5);response.setContentType("text/plain");PrintWriter out = response.getWriter();
if (session.isNew()) {count++;}out.println("This site has been accessed " + count + " times.");
}
public void destroy() {super.destroy();try {
dao.save(count);
8/8/2019 Servlet&Jsp Process
13/40
} catch (Exception e) {e.printStackTrace();
}}
}
This code will read the counter from a file on the server and return plain text to the
browser. The servlet will increase the counter if the user was 5 seconds inactive.
5.5. Run
Select your servlet, right-click on it and select Run As -> Run on Server.
Select your server and include your servlet so that is runs on the server.
8/8/2019 Servlet&Jsp Process
14/40
8/8/2019 Servlet&Jsp Process
15/40
8/8/2019 Servlet&Jsp Process
16/40
8/8/2019 Servlet&Jsp Process
17/40
}RequestDispatcher view = request.getRequestDispatcher(forward);view.forward(request, response);
}}
This controller will check which parameters has been passed to the servlet and then
forward the request to the correct JSP.
6.4. Create the Views (JSP)
Select the folder "WebContent", right-mouse click -> New -> JSP. Call the new JSP
"ShowAll". Select the "New JSP File (html)" template.
8/8/2019 Servlet&Jsp Process
18/40
Create the following coding.
Show all names
JimKnopf
JimBean
Create the JSP "Delete.jsp".
Insert title here
Delete successful
8/8/2019 Servlet&Jsp Process
19/40
Create the JSP "Edit.jsp".
Insert title here
First name:
Last name:
6.5. Run it
Run your new application by running "ShowAll.jsp" on the server. You should be able to
navigate between the pages.
7. Web Archive - How to create a war file from Eclipse
The following describes how to create a Web Archive (war) from Eclipse.
Right click on the project and select "Export".
8/8/2019 Servlet&Jsp Process
20/40
Specify the target directory and press finish.
8/8/2019 Servlet&Jsp Process
21/40
Import now the War file to your production Tomcat system and test the web application.
8/8/2019 Servlet&Jsp Process
22/40
How to develop webapplication in NETBEANS:-
Table of Contents
1. Creating a new Web Application
2. Creating a new JSP page
3. Creating a new Servlet
4.The web.xml file
5. Adding .jar files to your application
6. Restarting Tomcat
7. Debugging JSPs and Servlets
8. Exporting your Web Application
1.Creating a new Web Application
1. Start a new project and create and mount a directory for your web application.
2. With the above directory selected, select: File New JSPs & Servlets WebModule.
http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2791484http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2789953http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2790103http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2790308http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2841687http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2841776http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2841861http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2842010http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2791484http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2789953http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2790103http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2790308http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2841687http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2841776http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id2841861http://supportweb.cs.bham.ac.uk/documentation/java/servlets/netbeans-webapps/#id28420108/8/2019 Servlet&Jsp Process
23/40
8/8/2019 Servlet&Jsp Process
24/40
Now hit Finish.
4. Now you should have a view in your Filesystem explorer like this:
8/8/2019 Servlet&Jsp Process
25/40
8/8/2019 Servlet&Jsp Process
26/40
2. Hit Next.
8/8/2019 Servlet&Jsp Process
27/40
Enter a name for your JSP (without .jsp extension). You can select a subfolder to put it in
if you want to organise your pages into subfolders. Then hit Finish:
3. Netbeans creates a skeleton JSP page comprising of little more than the and
tags and a couple of commented-out sample bean directives. I have added the
and
lines in the screenshot below.
8/8/2019 Servlet&Jsp Process
28/40
4. To run your JSP page select it in the explorer or source editor and hit F6 or the
button. You will either see the page in Netbeans internal browser:
or you can point Mozilla at the relevent URL:
8/8/2019 Servlet&Jsp Process
29/40
Netbeans 3.6 uses port 8084 for Tomcat so the URL will be of the formathttp://localhost:8084//
3.Creating a new Servlet
1. To create a servlet, select the WEB-INF/classes folder or the corresponding mount at the
bottom, then do File New JSPs & Servlets Servlet.
8/8/2019 Servlet&Jsp Process
30/40
Hit Next
2. Enter a name for your servlet. You must specify a package for your servlet classes.
8/8/2019 Servlet&Jsp Process
31/40
Hit Next.
3. Specify a URL mapping for your servlet.
It can be useful to prefix servlet URLs with /servlet/ for later deployment on web
servers such as Apache where this prefix can be used for deciding which pages to
forward to web container such as Tomcat for processing.
You can also specify any servlet initialisation parameters that you can then access from
the servlet's init() method.
8/8/2019 Servlet&Jsp Process
32/40
Hit Finish
4. Netbeans creates a skeleton servlet with init(), destroy(), doGet(), doPost()
and getServletInfo() methods.
8/8/2019 Servlet&Jsp Process
33/40
By default the familiardoGet() and doPost() methods are both forwarded to a single
common processRequest() method as shown above. But you can delete this if you
want and code the appropriate doGet() and doPost() method bodies as appropriate.
5. To run your servlet select it in the explorer or the source editor and hit F6 or the
button and Netbeans should start Tomcat. Fire up your browser and point it at therelevant URL thus:
8/8/2019 Servlet&Jsp Process
34/40
4.The web.xml file
The web.xml file is the Web Application Deployment Descriptor, which defines which servlets
should be run for certain URLs and some other parameters of your web application.
Netbeans creates one for you when you create a new web application and it looks something like
this:
8/8/2019 Servlet&Jsp Process
35/40
The format of the URL-to-servlet mappings is described here.
You can see that Netbeans has automatically added and entries
forMyServlet we just created above.
The contains a definition for the time in minutes before a user's session
times out (in this case 30 minutes) and a section describing which files willbe loaded as the default home page for the web application. In this case it will try index.jsp,
index.html and finally if these don't exist index.htm.
http://supportweb.cs.bham.ac.uk/documentation/java/servlets/socs-tomcat/#id213976http://supportweb.cs.bham.ac.uk/documentation/java/servlets/socs-tomcat/#id2139768/8/2019 Servlet&Jsp Process
36/40
When you create a new servlet you can set up URL mappings in the New-Servlet dialogs and
Netbeans will add the appropriate entries to yourweb.xml, but you are free to edit these by hand.
5.Adding .jar files to your application
If your web applications needs additional libraries such as for database access then copy the
corresponding .jar file to your project's WEB-INF/lib directory (e.g. postgresql.jar) and
then right-click on WEB-INF/lib and select Refresh Folder and the file will be added to your
web application's classpath.
I have not found an easy way to copy files from within Netbeans. If you copy and paste a jar file
between mounts it tends to unpack the jar file, so I usually copy them in by hand e.g.:
cp /bham/common/java/lib/postgresql.jar ~/work/mywebapp/WEB-INF/lib/
Then subsequently, right-click on WEB-INF/lib and select Refresh Folder
6.Restarting Tomcat
1. If you edit your code, hitting (run) again will recompile and restart Tomcat.
2. To restart the whole web application select WEB-INF in the explorer and hit (run).
Any altered JSP/servlets will be recompiled and redeployed.
3. Occasionally not all changes will be registered, such as if you manually edit web.xml or
edit tag libraries. In which case I manually restart Tomcat by going to the Runtime tab
and right-click on the node Server Registry Tomcat 5 Servers http://localhost:8084 and select Start / Stop Server
8/8/2019 Servlet&Jsp Process
37/40
8/8/2019 Servlet&Jsp Process
38/40
8/8/2019 Servlet&Jsp Process
39/40
8/8/2019 Servlet&Jsp Process
40/40
The green line shows the current line to be executed and the execution can be controlled
by the buttons at the top:
The buttons left to right are Stop, Pause, Continue, Step Over, Step Into, Step
Out and Run to Cursor. (Pause is greyed out as the program is already paused at the
breakpoint).
In the panes at the bottom you can see the Call Stack, the values of Local Variables (youcan expand objects to look at member variables) and the values of any Watches you have
set. You can set a new watch on a variable by right-clicking on it and selecting New
Watch.
For debugging web applications Netbeans also provides a HTTP Monitor so that you canlook at the values of parameters in the actual HTTP requests.
3. To see the servlet code generated for a JSP page (remember JSPs are just templates for
servlets created automatically by the container), right click anywhere in the source of theJSP and select View Servlet.
8.Exporting your Web Application
To create a web application .war file to deploy on external JSP/Servlet containers, right-click on
WEB-INF and select Export WAR file... then give it a name in the file dialog.