Thursday, August 30, 2012

Introduction to servlet

Download Tomacat from

http://tomcat.apache.org/download-55.cgi



Extract the .gz file then its new folder with the name : apache-tomcat-5.5.35 o



Open the tomcat folder then go the tomcat-user.xml




Create new user  in tomcat-user.xml



           

           

           

           

       

       

       

        



Save the tomcat-user.xml



By default tomcat takes the port number 8080, if port number already busy with some other server you can change the tomcat port number in server.xml

Please replace the port 8080 to 8088 (some other port number)




Save the file, to start you tomcat server go the console

Change directory to : /opt/apache-tomcat-5.5.35/bin/startup.sh



When you enter the server gets started.


Open the browser type the url :http://localhost:8088, then it gives server welcome page.



When on Tomcat Manager it ask user name and password, please enter your tomcat use rname and password.



Then it display all the projects which you deploy in the server.




To create hello world example first create the java project in the eclipse




Click on finish, Then right click on your project create new folder with name
 WEB-INF
           |
           | ----classes(folder)
           |
           | ----lib(folder)
           |       |------ servlet-api.jar
           |
           |---- web.xml

Change source folder bin to classes and set the your servlet-api.jar in the class path

Right click on your project then go the properties



Then to the Build path



Change the default output folder to the  helloworld/WEB-INF/classes


Setting servlet-api.jar in the classpath

Click on libraries tab, then select add jar



Open the xml file, copy the existing web.xml doc type and paste in the xml file


web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-              app_2_4.xsd"  version="2.4">

    <display-name>welcome servlet</display-name>

    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>



Create html file with the name index.html


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="hello">Click Here</a>
</body>
</html>


Create servlet WelcomeServlet.java


package com.jsl.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WelcomeServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

resp.setContentType("text/html");
PrintWriter out=resp.getWriter();

out.print("<html>");
out.print("<head><title>Welcome to servlet world</title></head>");
out.print("<body>");
        out.print("<h3>Welcome to Servlet world! Today date is :"+new Date()+"</h3>");
out.print("</body>");
out.print("</html>");
}
}



Configure the servlet int the web.xml (deployment descriptor)



 <servlet>
    <servlet-name>welcome</servlet-name>
    <servlet-class>com.jsl.servlet.WelcomeServlet</servlet-class>
   </servlet>
 
    <servlet-mapping>
    <servlet-name>welcome</servlet-name>
    <url-pattern>/hello</url-pattern>
    </servlet-mapping>



Now everything is ready, We need to deploy the project into server

Go to the : /opt/apache-tomcat-5.5.35/conf/Catalina/localhost

and create  somename.xml [welcome.xml]

open the .xml file

 <Context docBase="/home/miani/Desktop/servlet/helloworld" reloadable="true"/>


 Start the server : http://localhost:8088/manager/html

Your project will be display on Tomcat web application Manager



 Select your project and click then it opens the welcome page



Click on hyper link it show the servlet output on the browser



Example with the Httpservlet


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Registration Form</title>
</head>
<body>
<form action="registration" method="post">
<pre>
username:<input type="text" name="userName">
emailid :<input type="text" name="email">
password:<input type="password" name="password">
Gander: <input type="radio" name="sex" value="Male" />Male<input type="radio" name="sex" value="Female" />Female
<input type="checkbox" name="lang" value="en" checked />English<input type="checkbox" name="lang" value="noen" /><span>Non English</span>
<input type="submit" value="submit">
</pre>
</form>
</body>
</html>



RegistrationServlet.java



package com.jsl.servlet;

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 org.apache.catalina.tribes.util.Arrays;

 public class RegistrationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name=req.getParameter("userName");
String email=req.getParameter("email");
String password=req.getParameter("password");
String sex=req.getParameter("sex");
String arr[]=req.getParameterValues("lang");
PrintWriter out=resp.getWriter();
resp.setContentType("text/html");

                        out.println("<html>");
out.print("<head><title>Welcome page</title></head>");
out.print("<body>");
out.print("<h3> Welcome ! "+name+"</h3>");
out.print("<table border='2'>");
out.print("<tr><td>Name:</td><td>"+name+"</td></tr>");
out.print("<tr><td>Email:</td><td>"+email+"</td></tr>");
out.print("<tr><td>Password:</td><td>"+password+"</td></tr>");
out.print("<tr><td>Sex:</td><td>"+sex+"</td></tr>");
out.print("<tr><td>Language:</td><td>"+Arrays.toString(arr)+"</td></tr>");
out.print("</table>");
out.print("</body>");
out.print("</html>");
}
}

web.xml



 <servlet>
  <servlet-name>registrationInfo</servlet-name>
  <servlet-class>com.jsl.servlet.RegistrationServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>registrationInfo</servlet-name>
  <url-pattern>/registration</url-pattern>
  </servlet-mapping>

No comments:

Post a Comment

Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...