By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,454 Members | 1,804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,454 IT Pros & Developers. It's quick & easy.

How to call servlet from JSP FORM ?

P: 28
Hi,

For Login authentication.
i create JSP Form.
login.jsp(webapps/loginsystem/login.jsp)
Expand|Select|Wrap|Line Numbers
  1. <form method="POST" action ="http://localhost:8085/loginsystem/lo"  name="login">
  2.  
  3. <font color="#FFFFFF" face="Bauhaus 93" size="4"><-- BACK</font></a></p>
  4. <div align="center">
  5.   <center>
  6.  
  7. <div align="center">
  8.   <center>
  9. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#0099cc" width="51%" id="AutoNumber1" bgcolor="#0099CC" height="134">
  10.   <tr>
  11.     <td width="100%" colspan="3" height="19">
  12.       <p align="center">
  13.       <b><font face="Verdana" color="#00FF00">ADMIN LOGIN 
  14.       </font></b></p>
  15.  
  16.     </td>
  17.   </tr>
  18.   <tr>
  19.     <td width="30%" height="23"><font face="Verdana" size="2" color="#800080">
  20.     USER NAME</font></td>
  21.     <td width="72%" colspan="2" height="23"><font color="#800080"><input type="text" name="user" size="20" onload = 'T1.focus();'></font></td>
  22.   </tr>
  23.    <script>
  24.     focus();
  25.   </script>
  26.  
  27.   <tr>
  28.     <td width="30%" height="23"><font face="Verdana" size="2" color="#800080">
  29.     PASSWORD</font></td>
  30.     <td width="72%" colspan="2" height="23"><font color="#800080"><input type="password" name="pass" size="20">   
  31.     </font> </td>
  32.   </tr>
  33.  
  34.   <tr>
  35.     <td width="30%" height="23"> </td>
  36.     <td width="72%" colspan="2" height="23"> </td>
  37.   </tr>
  38.   <tr>
  39.     <td width="30%" height="26"> </td>
  40.     <td width="18%" height="26">
  41.     <p align="left"><font color="#800080"><input type="submit" value="Login" name="B1" onclick = 'return checkNull();'></font></td>
  42.  

loginS.java[ServletCode](webapps/WEB-INF/classes/loginS.java)
Expand|Select|Wrap|Line Numbers
  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.sql.Connection;
  4. import java.sql.*;
  5. import javax.servlet.http.*;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. public class loginS extends HttpServlet {
  12.  
  13.     public loginS() {
  14.         super();
  15.     }
  16.  
  17.     public void destroy() {
  18.         super.destroy(); 
  19.  
  20.     }
  21.  
  22.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  23.             throws ServletException, IOException {
  24.  
  25. String a=request.getParameter("user");
  26. String b=request.getParameter("pass");
  27. HttpSession s=request.getSession();
  28. loginS l =(loginS)s.getAttribute("a");
  29.  
  30. System.out.println(a);
  31.     try
  32.         {
  33.         DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  34.         Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","password");
  35.  
  36.                         Statement st=null;
  37.             String c="select * from dil_Register where dil_username='"+a+"' AND dil_password='"+b+"'";
  38.             st=con.createStatement();
  39.             ResultSet rs=st.executeQuery(c);
  40.  
  41.             if(rs.next())
  42.             {
  43.                 response.sendRedirect("success.jsp");
  44.             }
  45.             else
  46.             response.sendRedirect("login.jsp");
  47.         }
  48.     catch(Exception e){}
  49.     }
  50.     public void init() throws ServletException {
  51.     }
  52. }
  53.  
  54.  
web.xml(webapps/loginsystem/WEB-INF/web.xml)

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?xml version="1.0" encoding="ISO-8859-1"?>
  3.  
  4. <!DOCTYPE web-app
  5.     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  6.     "http://java.sun.com/dtd/web-app_2_3.dtd">
  7.  
  8. <web-app>
  9.     <servlet>
  10.         <servlet-name>loginS</servlet-name>
  11.         <servlet-class>loginS</servlet-class>
  12.     </servlet>
  13.     <servlet-mapping>
  14.         <servlet-name>loginS</servlet-name>
  15.         <url-pattern>/lo</url-pattern>
  16.     </servlet-mapping>
  17.   <!--<welcome-file-list>
  18.     <welcome-file>home.html</welcome-file>
  19.   </welcome-file-list>!-->
  20. </web-app>
  21.  
  22.  
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..,
Feb 2 '11 #1
Share this Question
Share on Google+
2 Replies


nathj
Expert 100+
P: 938
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
Feb 10 '11 #2

P: 28
I got the Result(output).
Thanks for keeping me in ur mind.
Thanks.,Thanks for ur help..,

my programs are coming below..,


AuthenticationLogin.jsp

Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" %>
  2. <html>
  3. <head>
  4. <title>Login Page</title>
  5. <script language = "Javascript">
  6. function Validate(){
  7. var user=document.frm.user
  8. var pass=document.frm.pass
  9.  
  10. if ((user.value==null)||(user.value=="")){
  11. alert("Please Enter user name")
  12. user.focus()
  13. return false
  14. }
  15. if ((pass.value==null)||(pass.value=="")){
  16. alert("Please Enter password")
  17. pass.focus()
  18. return false
  19. }
  20. return true
  21. }
  22. </script>
  23.  
  24. <body bgcolor="White">
  25.  
  26. <!--<div align="center"><h2>Project Master!-->
  27. <br>
  28. </h1>
  29. <title>Master</title>
  30. <style rel="stylesheet" type="text/css" media="screen">
  31. form 
  32. {
  33.     margin:  6px auto;
  34.     width:  75%;
  35. }
  36. </style>
  37. </head>
  38. <form name="frm" action="/NewForm/LoginAuthentication" method="Post" onSubmit="return Validate()" >
  39. <div align="center">
  40. <table border='0' cellpadding="10" colspan="5" height="400" width="850" text='black' bgcolor="white">
  41. <tr><th colspan="5"><font color="DarkBlue"><h1><img src='report.png' width='250' height='150' border='0'><th></tr>
  42.  
  43. <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>
  44. <img src="usernamecopy.png" width="75" height="12" border="0"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="user" value=""/><br><br>
  45. <img src="passwordcopy.png" width="70" height="12" border="0"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="password" name="pass" value=""/><br>
  46. <br>&nbsp;&nbsp;&nbsp;<input type="submit" value="Sign-In" />
  47. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="Reset" />
  48. </td></tr>
  49. <tr><td></td></tr>
  50. </form>
  51. </body>
  52. </html>
  53.  

LoginAuthentication.java

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3. import java.sql.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6.  
  7. public class LoginAuthentication extends HttpServlet{
  8.  
  9.   private ServletConfig config;
  10.  
  11.   public void init(ServletConfig config)
  12.     throws ServletException{
  13.      this.config=config;
  14.      }
  15.   public void doPost(HttpServletRequest request, HttpServletResponse response) 
  16.               throws ServletException,IOException{
  17.  
  18.     PrintWriter out = response.getWriter();
  19.         Connection connection=null;
  20.     ResultSet rs;
  21.     String userName= request.getParameter("user");
  22.     String passwrd= request.getParameter("pass");
  23.  
  24.  
  25.     response.setContentType("text/html");
  26.     try {
  27.        // Load the database driver
  28.        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  29.            // Get a Connection to the database
  30.       connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","password");
  31.  
  32.       //Add the data into the database
  33.       String sql = "select dil_username,dil_password from dil_Register where dil_username = '"+userName+"' AND dil_password='"+passwrd+"' ";
  34.       Statement s = connection.createStatement();
  35.       //s.executeQuery (sql);
  36.  
  37.       //rs = s.getResultSet();
  38.       rs=s.executeQuery (sql);
  39.       if(userName.equals("vh") && passwrd.equals("vh123")){
  40.  
  41.       if (rs.next ()){
  42.  
  43.             out.println("<html>");
  44.             out.println("<head>");
  45.             out.println("<title>Master</title>");
  46.             out.println("<style rel='stylesheet' type='text/css' media='screen'> form { margin:  000.1em auto; width:  75%; } </style>");
  47.             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'>");      
  48.             out.println("<div align='right'> Welcome\t"+userName+",\t<a href='http://localhost:8085/NewForm/AuthenticLogin.jsp'>Logout</a>");
  49.             out.println("<U>");
  50.             out.println("<form>");
  51.             out.println("<table cellspacing='75' cellpadding='10' border='0' height='500' width='1000' text='black' colspan='4' bgcolor='white'>");
  52.             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>");
  53.             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>");
  54.             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>");
  55.             out.println("</tr> </table> </form> </body> </html>");
  56.  
  57.                    }
  58.       }
  59.       else{
  60.         out.println("<html>");
  61.             out.println("<head>");
  62.             out.println("<title>Master</title>");
  63.             out.println("<style rel='stylesheet' type='text/css' media='screen'> form { margin:  000.1em auto; width:  75%; } </style>");
  64.             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'>");      
  65.             out.println("<div align='right'> Welcome\t"+userName+",\t<a href='http://localhost:8085/NewForm/AuthenticLogin.jsp'>Logout</a>");
  66.             out.println("<form>");
  67.             out.println("<table cellspacing='75' cellpadding='10' border='0' height='500' width='1000' text='black' colspan='4' bgcolor='white'>");
  68.             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>");
  69.             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>");
  70.             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>");
  71.             out.println("</tr> </table> </form> </body> </html>");
  72.         //out.println("Please enter correct username and password");
  73.         //out.println("<a href='AuthenticLogin.jsp'><br>Login again</a>");
  74.       }
  75.  
  76.       //rs.close ();
  77.       s.close ();
  78.       }catch(Exception e){
  79.       System.out.println("Exception is ;"+e);
  80.       }
  81.   }
  82. }  
  83.  
  84.  

web.xml

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2.  
  3. <!DOCTYPE web-app
  4.     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  5.     "http://java.sun.com/dtd/web-app_2_3.dtd">
  6.  
  7. <web-app>
  8.     <servlet>
  9.         <servlet-name>LoginAuthentication</servlet-name>
  10.         <servlet-class>LoginAuthentication</servlet-class>
  11.         </servlet>
  12.     <servlet-mapping>
  13.         <servlet-name>LoginAuthentication</servlet-name>
  14.         <url-pattern>/LoginAuthentication</url-pattern>
  15.         </servlet-mapping>
  16. </web-app>
  17.  
i'm very Happy for having such a lovely friend like U.
Feb 11 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.