Java server Page and Java Servlet are both Web applications. Java Server Page or (JSP) is combination of HTML and Java script programming language. It is a dynamic process taking input through HTML and Java script coding online or over the internet and give a response to the client page. And Java Servlet actually "a special piece of Java code" this is also the same way taking the request from the internet and give a response to the client page.In this page I am going to describe only how to run a Java scripts, managing Java Servlet Web Folders and get in to work online through Apache server. Also, how to retrieve Data and store data in database Server. However, not the software installation and configuration viewpoint. Though, I installed Apache server and Tomcat with Netbeans software bundle. Now My SQL and MySQL database connector available at Oracle website. As an example, I will use one of my course assignment I have done during taking the Enterprise level Java Development programming course.
Components I used for Java Servlet and Java Server Page get Work online:
Apache Tomcat
Netbeans with Apache server( to create and edit Java script )
MySQL or other Database to connect and store data
Database Connector
SQL scripts to create Relational Database
Preparing to get Servlet/JSP work:
First, check the Apache server is work and successfully installed. open the Command line prompt and Run the Command for Apache server:
Catalina Run and Enter Here is an screen shot image of successfully Apache and Tomcat installed in my computer :
Above screen image showing all the Environment Variable need to set up to get apache server run. please Click here to see how to set up path Variable. also, to get in online and verify Apache server works, open a browser and type local host and web port number:- http://localhost:8080/. if it is installed correctly then able to see this page of Apache Tomcat.
Second, and the next step to create folders and files for deployment of Web application. Creating folders and managing Web and classes are little different between Java server page and Java servlet.
Java Servlet
I used NetBeans to write and run the Java servlet to get the class file. First need to activate the NetBeans for web applications. Here how I create a web application project in netbeans: Below screen shot image showing first chhose web application for the project, then project name JavaServletNB and choose Location C:/ and then next:
for the Server setting choose Apache Tomcat which is already configured during installation, Java EE and existing context path, though what I have learned, customized and setting context path manually later I will show and explain how configure and setup context path in server.xml file.
for the servlet need choose file type Java and Java class:
gave my Java class name, HelloWorldServlet.java, its same as java program, for the servlet already hada ready made abstract class which is HttpServlet that extends here in this helloWorldServlet. also used doGet Method to get the request online and response through the apache server. here is sample of java class code for HelloWorldServlet:
Build this Java class code, run the web application project and get the class(HelloWorldServlet.class) file from netbeans build folder. here is a sample image how netbeans created all the web application folders:
Now is ready to create a customized Deployment folders to get the Servlet work online,and manually configure the Server.xml and Web.xml files, Here how I created my deployment folders:
folder name myPortfolio the location is C:/.
Create another folder name /Servlet
Create a WEB-INF folder under /Servlet/WEB-INF
now create a classes folder under /WEB-INF/classes
this is my full path of where classes folders are: C:\myPortfolio\Servlet\WEB-INF\classes
Now copy HelloWorldServlet.class file and put it in to this classes folder. also, I put the web.xml file under the Servlet folder the same level of WEB-INF folders, Here is how I configured the web.xml file to get the Servlet online:
in <url-pattern> element gave /Servlet so online invoke the HelloWorldServlet under the /Servlet directory.
Also, Need to set up a context path to show where all the deployment folders are located. for that reason need to confogure the server.xml file
Here where is the server.xml file located in my computer, it is under program file and apache configuration folder:
C:\Program Files\Apache\Tomcat\conf, below an image of my server.xml file entry of Context Path:
this is an entry of server.xml file : <Context path="/welcome" docBase="C:/myPortfolio/Servlet" debug="0" reloadable="true" />
created an alias like /welcome and then gave full path of where is my web folder under /welcome alias. HelloWrldServlet is ready now to get in online, here how steps I followed :
Run the Apache server
Open a browser and type web port where is apache server and context path alias created in server.xml and the directory /Servlet created in web.xml here is full url:- http://localhost:8080/welcome/Servlet
here is an image how my servlet get online :
Summary
Create Java class and run the servlet in netbeans
Create and customized deployment folder copy and paste class and web files accordingly
Configure context path in server.xml
run Apache
finally tested on a online browser