473,466 Members | 4,850 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to call servlet from JSP FORM ?

28 New Member
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
2 17318
nathj
938 Recognized Expert Contributor
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
jerald m
28 New Member
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

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

Similar topics

2
by: BUX | last post by:
I have to call Forms!myForm.myControl_AfterUpdate using myString = "Forms!myForm.myControl_AfterUpdate". I have tried with Application.Run myString Call myString eval myString
3
by: David N | last post by:
Hi All, I just wonder if in C#, I can develop a user defined control that can call its parent function which is not yet developed. For example, how do I make my user control call a...
5
by: Steven C | last post by:
Hello: I've seen several recent threads on this, but I'm still not clear how to reference a *method* on one form from another. I want to add a customer in the child form, and then update the...
5
by: Aaron Ackerman | last post by:
I have a form that I need to call that is in it's own project in the SAME solution and I am at a loss. So what I am trying to do is call Form1 in Project 1 from Form2 in Project2. How do I do...
2
by: Robert Gage | last post by:
Hello All Can anyone tell me in a single solution how to call a form in another project? I have a large solution made up of several projects and need to call back and forth betweem them TIA
2
by: khokimfang | last post by:
Hi All, I want to ask how to call form with only one variable in VB.Net. I have 3 form (Form1, Form2 and Form3) and 1 module. in the module i want to declare public variable to call form for ex:...
4
by: Eric | last post by:
Hi: Is it possible i call access form in browser. Thanks,
1
by: abhijitkonwar | last post by:
how can i call a form from another form? please help!!
5
by: senthilkumarb | last post by:
How can i call a function automatically when the server started ?
4
by: varunbhatia87 | last post by:
I am developing an application in vb.net, in which i m using a treeview control which display nodes generated from database, when i select a node it gives me a form name that comes also from...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.