Servlet Programming in JAVA
Servlet Programming in JAVA
Web Application:
• A web application is an application accessible from the web.
• A web application is composed of web components like
Servlet, JSP, etc. and other elements such as HTML, CSS, and
JavaScript.
• The web components typically execute in Web Server and
respond to the HTTP request.
Servlet-api.jar
Two ways to load the jar file
1. set classpath
2. paste the jar file in JRE/lib/ext folder
Put the java file in any folder. After compiling the java file,
paste the class file of servlet in WEB-INF/classes directory.
4.Create a deployment description:
• Deployment description is an XML file with a name web.xml
• From this xml file web container gets the information about the servlet
to be invoked
Web.xml
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>First</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
5.Start the server and deploy the project:
• There are two ways to deploy the project
– Deployment
• Copy project into c://xampp/tomcat/web-apps
– Start Tomcat server
• By using Xampp control panel
• By running catalina_start batch file.
(c://xampp/tomcat/)
6. Access the servlet:
Reading Servlet Parameters:
• The Servlet Request class includes methods that allow you to read
the names and values of parameters that are included in a client
request.
Example: “index.html”
<html>
<body>
<form action="home" method="get">
Enter your name :
<input type="text" name="name"><br>
<input type="submit" value="OK">
</body>
</html>
import javax.servlet.http.*; “ReadingPar.java”
import javax.servlet.*;
import java.io.*;
public class ReadingPar extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse
res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
String name=req.getParameter("name");
pw.println("Welcome "+name);
}}
Compilation:
Javac ReadingPar.java
After compiling the java file, paste the class file of servlet in
WEB-INF/classes directory.
“web.xml”
<web-app>
<servlet>
<servlet-name>SPrg2</servlet-name>
<servlet-class>ReadingPar</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SPrg2</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
</web-app>
Example: “index.html”
<html>
<head>
<title>Calculator</title>
</head>
<body>
<form action="/SPrg3/evaluate" method="post" name="frm">
Enter num1:<input name="txt1" type="text" /><br>
Enter num2:<input name="txt2" type="text" /><br>
Operator
<select name="op">
<option value="+">Addition</option>
<option value="-">Subtraction</option>
<option value="*">multiplication</option>
<option value="/">division</option>
<option value="%">remainder</option>
</select><br>
<input type="submit" value="submit" />
</form>
</body>
</html>
“Calculator.java”
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;
public class Calculator extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String n1 = request.getParameter("txt1");
String n2 = request.getParameter("txt2");
String op = request.getParameter("op");
if(op.equals("+")){
out.println("SUM IS :" +(Integer.parseInt(n1) + Integer.parseInt(n2)));
}
else if(op.equals("-")){
out.println("SUB IS :" +(Integer.parseInt(n1) - Integer.parseInt(n2)));
}
else if(op.equals("*")){
out.println("MUL IS :" +(Integer.parseInt(n1) * Integer.parseInt(n2)));
}
else if(op.equals("%")){
out.println("MOD IS :" +(Integer.parseInt(n1) % Integer.parseInt(n2)));
}
else{
out.println("DIV IS :" +(Integer.parseInt(n1) / Integer.parseInt(n2)));
}
} }
“web.xml”
<web-app>
<servlet>
<servlet-name>SPrg3</servlet-name>
<servlet-class>Calculator</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SPrg3</servlet-name>
<url-pattern>/evaluate</url-pattern>
</servlet-mapping>
</web-app>