How to call servlet from JSP FORM ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jerald m
    New Member
    • Dec 2010
    • 28

    How to call servlet from JSP FORM ?

    Hi,

    For Login authentication.
    i create JSP Form.
    login.jsp(webap ps/loginsystem/login.jsp)
    Code:
    <form method="POST" [B]action ="http://localhost:8085/loginsystem/lo"[/B]  name="login">
    
    <font color="#FFFFFF" face="Bauhaus 93" size="4"><-- BACK</font></a></p>
    <div align="center">
      <center>
    
    <div align="center">
      <center>
    <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#0099cc" width="51%" id="AutoNumber1" bgcolor="#0099CC" height="134">
      <tr>
        <td width="100%" colspan="3" height="19">
          <p align="center">
          <b><font face="Verdana" color="#00FF00">ADMIN LOGIN 
          </font></b></p>
    
        </td>
      </tr>
      <tr>
        <td width="30%" height="23"><font face="Verdana" size="2" color="#800080">
        USER NAME</font></td>
        <td width="72%" colspan="2" height="23"><font color="#800080"><input type="text" name="user" size="20" onload = 'T1.focus();'></font></td>
      </tr>
       <script>
    	focus();
      </script>
    
      <tr>
        <td width="30%" height="23"><font face="Verdana" size="2" color="#800080">
        PASSWORD</font></td>
        <td width="72%" colspan="2" height="23"><font color="#800080"><input type="password" name="pass" size="20">   
        </font> </td>
      </tr>
    
      <tr>
        <td width="30%" height="23"> </td>
        <td width="72%" colspan="2" height="23"> </td>
      </tr>
      <tr>
        <td width="30%" height="26"> </td>
        <td width="18%" height="26">
        <p align="left"><font color="#800080"><input type="submit" value="Login" name="B1" onclick = 'return checkNull();'></font></td>

    loginS.java[ServletCode](webapps/WEB-INF/classes/loginS.java)
    Code:
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.*;
    import javax.servlet.http.*;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class loginS extends HttpServlet {
    
    	public loginS() {
    		super();
    	}
    
    	public void destroy() {
    		super.destroy(); 
    
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    String a=request.getParameter("user");
    String b=request.getParameter("pass");
    HttpSession s=request.getSession();
    loginS l =(loginS)s.getAttribute("a");
    
    System.out.println(a);
    	try
    		{
    		DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    		Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","password");
    
    						Statement st=null;
    			String c="select * from dil_Register where dil_username='"+a+"' AND dil_password='"+b+"'";
    			st=con.createStatement();
    			ResultSet rs=st.executeQuery(c);
    
    			if(rs.next())
    			{
    				response.sendRedirect("success.jsp");
    			}
    			else
    			response.sendRedirect("login.jsp");
    		}
    	catch(Exception e){}
    	}
    	public void init() throws ServletException {
    	}
    }
    web.xml(webapps/loginsystem/WEB-INF/web.xml)

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
    
    <web-app>
    	<servlet>
            <servlet-name>loginS</servlet-name>
            <servlet-class>loginS</servlet-class>
        </servlet>
    	<servlet-mapping>
            <servlet-name>loginS</servlet-name>
            <url-pattern>/lo</url-pattern>
        </servlet-mapping>
      <!--<welcome-file-list>
        <welcome-file>home.html</welcome-file>
      </welcome-file-list>!-->
    </web-app>
    i gave in form ACTION="http://localhost:8085/loginsystem/lo"

    but after i submit the form it is in the same FORM not go for Servlet.,
    wat i need to do..,
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Hi,

    Forgive my ignorance here but I'll try to help as best I can.

    When I ahve tried to do this I have used Struts and the combination of the web.xml file and the struts-config.xml has anabled me to achieve exactly what you are looking for.

    Have a read around Apache Struts and try out some of the examples. I'm sure you'll be up and running soon.

    Cheers
    nathj

    Comment

    • jerald m
      New Member
      • Dec 2010
      • 28

      #3
      I got the Result(output).
      Thanks for keeping me in ur mind.
      Thanks.,Thanks for ur help..,

      my programs are coming below..,


      AuthenticationL ogin.jsp

      Code:
      <%@ page language="java" %>
      <html>
      <head>
      <title>Login Page</title>
      <script language = "Javascript">
      function Validate(){
      var user=document.frm.user
      var pass=document.frm.pass
      
      if ((user.value==null)||(user.value=="")){
      alert("Please Enter user name")
      user.focus()
      return false
      }
      if ((pass.value==null)||(pass.value=="")){
      alert("Please Enter password")
      pass.focus()
      return false
      }
      return true
      }
      </script>
      
      <body bgcolor="White">
      
      <!--<div align="center"><h2>Project Master!-->
      <br>
      </h1>
      <title>Master</title>
      <style rel="stylesheet" type="text/css" media="screen">
      form 
      {
          margin:  6px auto;
          width:  75%;
      }
      </style>
      </head>
      <form name="frm" action="/NewForm/LoginAuthentication" method="Post" onSubmit="return Validate()" >
      <div align="center">
      <table border='0' cellpadding="10" colspan="5" height="400" width="850" text='black' bgcolor="white">
      <tr><th colspan="5"><font color="DarkBlue"><h1><img src='report.png' width='250' height='150' border='0'><th></tr>
      
      <tr>&nbsp;&nbsp;&nbsp;&nbsp;<td></td><td></td><td><img src="distillogo.png" width="250" height="50" border="0"><br> <img src="layer8.png" width="250" height="50" border="0"></td><td><br>
      <img src="usernamecopy.png" width="75" height="12" border="0"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="user" value=""/><br><br>
      <img src="passwordcopy.png" width="70" height="12" border="0"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="password" name="pass" value=""/><br>
      <br>&nbsp;&nbsp;&nbsp;<input type="submit" value="Sign-In" />
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="Reset" />
      </td></tr>
      <tr><td></td></tr>
      </form>
      </body>
      </html>

      LoginAuthentica tion.java

      Code:
      import java.io.*;
      import java.util.*;
      import java.sql.*;
      import javax.servlet.*;
      import javax.servlet.http.*;
      
      public class LoginAuthentication extends HttpServlet{
      
        private ServletConfig config;
        
        public void init(ServletConfig config)
          throws ServletException{
           this.config=config;
           }
        public void doPost(HttpServletRequest request, HttpServletResponse response) 
                    throws ServletException,IOException{
            
          PrintWriter out = response.getWriter();
              Connection connection=null;
          ResultSet rs;
      	String userName= request.getParameter("user");
      	String passwrd= request.getParameter("pass");
      
          
          response.setContentType("text/html");
          try {
             // Load the database driver
             DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      	       // Get a Connection to the database
            connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","password");
      	   
            //Add the data into the database
            String sql = "select dil_username,dil_password from dil_Register where dil_username = '"+userName+"' AND dil_password='"+passwrd+"' ";
            Statement s = connection.createStatement();
            //s.executeQuery (sql);
            
      	  //rs = s.getResultSet();
      	  rs=s.executeQuery (sql);
      	  if(userName.equals("vh") && passwrd.equals("vh123")){
      
      	  if (rs.next ()){
      		
      			out.println("<html>");
      			out.println("<head>");
      			out.println("<title>Master</title>");
      			out.println("<style rel='stylesheet' type='text/css' media='screen'> form { margin:  000.1em auto; width:  75%; } </style>");
      			out.println("</head> <body bgcolor='White'> <a href='http://localhost:8085/NewForm/Form1.jsp'>ProjectMaster</a>\t\t<a href='http://localhost:8085/NewForm/ListDB.jsp'>List</a> <div align='center'><img src='logo.png' width='100' height='30' border='0'>");	  
      			out.println("<div align='right'> Welcome\t"+userName+",\t<a href='http://localhost:8085/NewForm/AuthenticLogin.jsp'>Logout</a>");
      			out.println("<U>");
      			out.println("<form>");
      			out.println("<table cellspacing='75' cellpadding='10' border='0' height='500' width='1000' text='black' colspan='4' bgcolor='white'>");
      			out.println("<tr><td><img src='logo.png' width='250' height='50' border='0'><br> <img src='layer8.png' width='250' height='50' border='0'></td>");
      			out.println("<td><font color='Black' size='2'><p>\t\tAs an organization, putting together a reporting and Business Intelligence solution is among the most important decisions a company makes. And making a valid business case for such a project is the stuff CIO nightmares are made of.</p>");
      			out.println("<p align='justify'>\t\tWalking the fine line between plain vanilla reporting and the high end statistical data analysis to bring forth value and business benefits from a staged approach to data management and analysis leading to information management is usually the success mantra in these initiatives.Distil BI and its inherent capabilities allow easier appreciation of the many intangibles you’ll find hard to articulate and justify in such projects. </p></td>");
      			out.println("</tr> </table> </form> </body> </html>");
      				  
      		   		}
      	  }
      	  else{
      		out.println("<html>");
      			out.println("<head>");
      			out.println("<title>Master</title>");
      			out.println("<style rel='stylesheet' type='text/css' media='screen'> form { margin:  000.1em auto; width:  75%; } </style>");
      			out.println("</head> <body bgcolor='White'> <a href='http://localhost:8085/NewForm/TimeForm.jsp'>Time Log</a> <div align='center'><img src='logo.png' width='100' height='30' border='0'>");	  
      			out.println("<div align='right'> Welcome\t"+userName+",\t<a href='http://localhost:8085/NewForm/AuthenticLogin.jsp'>Logout</a>");
      			out.println("<form>");
      			out.println("<table cellspacing='75' cellpadding='10' border='0' height='500' width='1000' text='black' colspan='4' bgcolor='white'>");
      			out.println("<tr><td><img src='logo.png' width='250' height='50' border='0'><br> <img src='layer8.png' width='250' height='50' border='0'></td>");
      			out.println("<td><font color='Black' size='2'><p>\t\tAs an organization, putting together a reporting and Business Intelligence solution is among the most important decisions a company makes. And making a valid business case for such a project is the stuff CIO nightmares are made of.</p>");
      			out.println("<p align='justify'>\t\tWalking the fine line between plain vanilla reporting and the high end statistical data analysis to bring forth value and business benefits from a staged approach to data management and analysis leading to information management is usually the success mantra in these initiatives.Distil BI and its inherent capabilities allow easier appreciation of the many intangibles you’ll find hard to articulate and justify in such projects. </p></td>");
      			out.println("</tr> </table> </form> </body> </html>");
              //out.println("Please enter correct username and password");
              //out.println("<a href='AuthenticLogin.jsp'><br>Login again</a>");
            }
      	  
            //rs.close ();
            s.close ();
            }catch(Exception e){
            System.out.println("Exception is ;"+e);
            }
        }
      }

      web.xml

      Code:
      <?xml version="1.0" encoding="ISO-8859-1"?>
      
      <!DOCTYPE web-app
          PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
          "http://java.sun.com/dtd/web-app_2_3.dtd">
      
      <web-app>
      	<servlet>
              <servlet-name>LoginAuthentication</servlet-name>
              <servlet-class>LoginAuthentication</servlet-class>
      		</servlet>
      	<servlet-mapping>
              <servlet-name>LoginAuthentication</servlet-name>
              <url-pattern>/LoginAuthentication</url-pattern>
      		</servlet-mapping>
      </web-app>
      i'm very Happy for having such a lovely friend like U.

      Comment

      Working...