Servlet By Prof B A Khivsara Note The

Servlet By Prof. B. A. Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and not for commercial use.

What are Servlets? • Java Servlets are programs that run on a Web or Application server and act as a middle layer between a requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server. • Using Servlets, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically.

Advantages of Servlet over CGI • Performance is significantly better. • Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request. • Servlets are platform-independent because they are written in Java. • servlets are trusted. • The full functionality of the Java class libraries is available to a servlet.

Servlets Architecture

Servlets Packages • Java Servlets are Java classes run by a web server that has an interpreter that supports the Java Servlet specification. • Servlets can be created using the packages • javax. servlet. http

Requirements Before running Servlet your machine requires following tools • 1> Eclipse (IDE-integrated development environment) • 2> Tomcat (Web Server)

How to configure tomcat server in Eclipse ? (One time Requirement) • If you are using Eclipse IDE first time, you need to configure the tomcat server First. • For configuring the tomcat server in eclipse IDE, • click on servers tab at the bottom side of the IDE -> right click on blank area -> New -> Servers -> choose tomcat then its version -> next -> click on Browse button -> select the apache tomcat root folder previous to bin -> next -> add. All -> Finish.

How to configure tomcat server in Eclipse ?

How to configure tomcat server in Eclipse ?

How to configure tomcat server in Eclipse ?

How to configure tomcat server in Eclipse ?

How to configure tomcat server in Eclipse ?

How to configure tomcat server in Eclipse ?

How to configure tomcat server in Eclipse ?

How to configure tomcat server in Eclipse ?

Steps to run servlet in Eclipse Create a Dynamic web project create a servlet add servlet-api. jar file Run the servlet

Steps to run servlet in Eclipse Create a Dynamic web project create a servlet add servlet-api. jar file Run the servlet

Create the dynamic web project Start Eclipse and In Menu : File -> New -> Dynamic Web Project

Create the dynamic web project

Create the dynamic web project

Create the dynamic web project

Create the dynamic web project Structure of Dynamic Web Project

Steps to run servlet in Eclipse Create a Dynamic web project create a servlet add servlet-api. jar file Run the servlet

Create a servlet Right click on Src -> New -> Sevlet

Create a servlet Give name of class

Create a servlet

Create a servlet Either select do. Get or do. Post

Create a servlet Servlet is created, you can write the code now.

Steps to run servlet in Eclipse Create a Dynamic web project create a servlet add servlet-api. jar file Run the servlet

Add servlet-api. jar file

Add servlet-api. jar file

Add servlet-api. jar file In Tomcat folder goto Lib folder and select servlet-api. jar file

Add servlet-api. jar file

Steps to run servlet in Eclipse Create a Dynamic web project create a servlet add servlet-api. jar file Run the servlet

Run the servlet Right click on project name -> click Run As -> Run on Server

Run the servlet

Run the servlet

Example 1 To Print Hello World directly import java. io. *; import javax. servlet. http. *; public class Hello. World extends Http. Servlet { public void init() throws Servlet. Exception { } public void do. Get(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { response. set. Content. Type("text/html"); Print. Writer out = response. get. Writer(); out. println("<h 1> Hello World </h 1>"); } public void destroy() { } }

Reading Form Data using Servlet • get. Parameter() − You call request. get. Parameter() method to get the value of a form parameter. • get. Parameter. Values() − Call this method if the parameter appears more than once and returns multiple values, for example checkbox. • get. Parameter. Names() − Call this method if you want a complete list of all parameters in the current request.

Example 2 To read data from HTML file and print that. • It requires 2 files: • 1. HTML (s 1. html)File • 2. Servlet (s 2. java) File • First Run HTML file and after clicking on submit button it will run servel(. java) file.

Example 2 To read data from HTML file and print that. • S 1. html (html code) <html> <form method=“post” action=“s 2”> Enter Your Name <input type=text name=“t 1”> <input type=“submit” value=“submit”> </form> </body> </html>

Example 2 To read data from HTML file and print that. • S 2. java (Servlet Code) import java. io. *; import javax. servlet. *; public class s 2 extends Http. Servlet { public void do. Post(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { response. set. Content. Type("text/html"); String a= request. get. Parameter("t 1"); Print. Writer out= response. get. Writer(); out. print(" Your Name is: "+a); } }

Example 3 – (HTML code) To read data from HTML checkbox values print that • • • <!DOCTYPE html> <html> Name of Servlet File <body> <form method="post" action=s 1> Hobbies <input type="checkbox" name="t 1" value=“Cricket"> Cricket <input type="checkbox" name="t 1" value="Music">Music <input type="checkbox" name="t 1" value=“Dance">Dance <input type="submit" value="submit"> </form> </body> </html>

Example 3 – (servlet code) To read data from HTML checkbox values print that • protected void do. Post(Http. Servlet. Request request, Http. Servlet. Response response) • throws Servlet. Exception, IOException { • response. set. Content. Type("text/html"); • Print. Writer out=response. get. Writer(); • String[] values=request. get. Parameter. Values("t 1"); • • for(int i=0; i<values. length; i++) • { • out. println("<li>"+values[i]+"</li>"); • } • }

References • https: //www. javatpoint. com/servlet-tutorial • https: //www. tutorialspoint. com/servlets/index. htm • https: //www. tutorialspoint. com/servlets-firstexample. htm • https: //www. javatpoint. com/creating-servlet-in-eclipse-ide
- Slides: 45