10-02-25 & 11-02-25
------------------------
Q) Develop a Java web application to implement the use-case of addition of two
numbers.
Step 1:- directory structure
additionapplication
WEB-INF
classes
src
Step 2:- application files development
=>Here we have two files.
1)numbers.html (web form)
2)AdditionServlet.java
numbers.html
----------------
Addition user interface
AdditionServlet.java
-----------------------
import javax.servlet.*;
public class AdditionServlet implements Servlet{
public void init(ServletConfig config){
}
public void destroy(){
}
public String getServletInfo(){
return null;
}
public void service(ServletRequest request,ServletResponse response){
int n1=Integer.parseInt(request.getParameter("t1"));
int n2=Integer.parseInt(request.getParameter("t2"));
int sum=n1+n2;
System.out.println("The sum is "+sum);
}
public ServletConfig getServletConfig(){
return null;
}
}
Note:- Before compiling this servlet source file, place servlet-api.jar file in classpath.
For eg.
SET CLASSPATH=C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib\servlet-api.jar
web.xml
----------
one
AdditionServlet
one
/add
Q)How many names does a servlet have in a Java web application?
1)registration name
2)class name
3)public URL name
Q)What is the purpose of tag in web.xml?
=>to register a servlet with the container.
Note:- Each servlet of the application should be registered with the
container.
Q)What is the purpose of tag?
=>to give public URL name to the servlet
Q)What is deployment of a Java web application?
=>Installing the web application into the web container so that
its services are accessible to web clients.
Q)How to deploy a Java web application into Tomcat server?
Step 1:- copy the application's root folder(along with all the files)
into "webapps" folder of Tomcat.
Step 2:- start the Tomcat server
Q)How to run the above application?
=>Type the following URL into the browser.
http://localhost:8081/additionapplication/numbers.html
Q)How to capture user input in a servlet?
=>By calling getParameter() method on the request object.
=>This method takes request parameter name as argument and
returns request parameter value as String.