Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Simple application form using doPost() method
Previous Home Next
Following is the HTML file which is design to take information from user.
<html>
 <head>
  <title></title>
  <meta http-equiv="Content-Type"
       content="text/html; charset=UTF-8">
 </head>
 <body>
  <h1 align="center" 
    style="font-size: 20pt; font-style: italic; color:green ">
	  <B>A Simle R4R Form</B></h1>
  <form name="form" method="get" action="NewR4R">
  <table>
   <tr>
   <td><B>Company Name:</B></td>
   <td>
   <input type=textbox name="companyName" 
         size="25" value="" maxlength="15"></td>
   <!in the servlet or JSP comment display in that manner >
   <!type=textbox=== declayer input from textbox>
   <!name=== declayer what is written when the program is compile >
   <!size=25 shows textfield area>
   <!maxlength=== declayer maximum digit entered into textfield>
   </tr>
   <tr>
   <td><B>WEB ID:</B></td>
   <td>
   <input type=textbox name="webId" 
      size="25" value="" maxlength="15"></td>
   </tr>
   </table>
   <input type=submit value="submit">
   <input type=reset value="reset">
   </form>
  </body>
</html>
Following is the servlet program
/*Program that take information from
newR4R.html and store it into server.*/

//save that file as a name of NewR4R.java
package r4r.co.in;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NewR4R extends HttpServlet {

 public void doGet
 (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

 //Taking response from newR4R.html
 response.setContentType("text/html");
 String compnyName =
	    (Servlet) request.getParameter("companyName");
 String WebID = (Servlet) request.getParameter("webId");
 //Now checked the validation of newR4R.html
 if (compnyName.equalsIgnoreCase("R4R Tech Soft")
	         && WebID.equalsIgnoreCase("www.r4r.co.in")
 && compnyName != null && WebID != null) {
//Call the home.java
response.sendRedirect("home");
  } else {
//call the Invalid.java
response.sendRedirect("Invalid");

  }

 }
}
web.xml mapping
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
 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">
	<display-name>Simple application form</display-name>
	<servlet>
		<description>
		</description>

		<servlet-name>NewR4R</servlet-name>
		<servlet-class>r4r.co.in.NewR4R</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>NewR4R</servlet-name>
		<url-pattern>/NewR4R</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app
Invalid.java
/*This page is call when the
validationCondition is False/Null on NewR4R1
*/
package r4r.co.in;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

//Page maping 
@WebServlet(name = "Invalid", urlPatterns = {"/Invalid"})
public class Invalid extends HttpServlet {
   public void service
	(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
       PrintWriter out = response.getWriter();
        try {
            /* CODE for output your page here*/
          out.println("<html>");
          out.println("<head>");
          out.println("<title>Servlet Invalid</title>");
          out.println("</head>");
          out.println("<body>");
          out.println("<h1>Please enter correct information</h1>");
          out.println("</body>");
		  out.println("</html>");

        } finally {
            out.close();
        }
    }
}
 
Home.java
/* jThis page is call when the
validationCondition is true on NewR4R1
*/
package r4r.co.in;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/* We can map servlet directly on our servlet file as
following .We can use web.xml for mapping .*/

@WebServlet(name = "Home", urlPatterns = {"/home"})

public class Home extends HttpServlet {
  public void service
	  (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* CODE for output your page here*/
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Home</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>WELCOME TO R4R</h1>");
            out.println("</body>");
            out.println("</html>");

        } finally {
            out.close();
        }
    }
}

Make a work space create a folder with name of WEB_INF with in WEB-INF create two folder with in it with name of classes and lib .compile all java files. put .class file into classes folder .place web.xml in work space .put all Jars in lib.Put html file out side WEB-INF.Place work space in tomcat in root folder.

Result: Run these file in your browser then you get..
After clicking submit button the browser opens
Previous Home Next