how to execute the below servlet with html page

how to execute the below servlet with html page

//vallidate user n pwd by validservlet


package active;

import javax.servlet.*;
import java.io.*;
import java.sql.*;


public class validservlet extends GenericServlet
{


Connection conn;
ServletConfig sc;
ServletContext scont;

public void init() throws ServletException 
{

try{

      sc=getServletConfig();
    scont=sc.getServletContext();

    String driver=scont.getInitParameter("driver");
    String url=scont.getInitParameter("url");
    String UNAME=sc.getInitParameter("UNAME");
    String PWD=sc.getInitParameter("PWD");

       Class.forName(driver);
    conn=DriverManager.getConnection(url,UNAME,PWD);

}
 catch(Exception e)
{
System.out.println(e.getMessage());
}

}

public void service(ServletRequest req, ServletResponse res) throws IOException,ServletException
{

res.setContentType("text/html");

PrintWriter out=res.getWriter();

try{

    String uname=req.getParameter("uname");
    String pwd=req.getParameter("pwd");

String sql="SELECT * FROM userlogin UNAME=? and PWD=?";

PreparedStatement pst=conn.prepareStatement(sql);

pst.setString(1,UNAME);
pst.setString(2,PWD);

ResultSet rs=pst.executeQuery();

if(rs.next())
{

RequestDispatcher rd=req.getRequestDispatcher("Admin.java");
rd.forward(req,res);

}
else
{

RequestDispatcher rd=req.getRequestDispatcher("userlogin.html");

//rd.forward(req,res);
rd.include(req,res);

System.out.println("<center><font color=red>invalid</font></center>");
}
}

catch(Exception e){}

}

public void destroy()
{

try
{conn.close();

}catch(Exception e){}

}



}


//Admin 


package active;
import javax.servlet.*;
import java.io.*;
import java.sql.*;

public class Admin extends GenericServlet
{

public void service(ServletRequest req,ServletResponse res)throws IOException,ServletException
{

res.setContentType("text/html");

PrintWriter out=res.getWriter();

System.out.println("<center> welcome to Admin </center>");

}

}

<web-app>

<content-param>
<param-name>driver</param-name>
<param-value>
sun.jdbc.odbc.JdbcOdbcDriver
</param-value>
</content-param>

<content-param>
<param-name>url</param-name>
<param-value>
jdbc:odbc:mydsn
</param-value>
</content-param>



<servlet>
<servlet-name>validservlet</servlet-name>
<servlet-class>active.validservlet</servlet-class>


<init-param>
<param-name>user</param-name>
<param-value>system</param-value>
</init-param>


<init-param>
<param-name>password</param-name>
<param-value>rammohan</param-value>
</init-param>

</servlet>


<servlet-mapping>
<servlet-name>validservlet</servlet-name>
<url-pattern>/ss</url-pattern>
</servlet-mapping>


<servlet>
<servlet-name>Admin</servlet-name>
<servlet-class>active.Admin</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>

</web-app>


//validate login.html


<html>
<body>
<body bgcolor="wheat">
<center>

<h1>Login Form</h1>

<form action="ss">
<Table>

<tr>
<td>enter name</td>
<td><Input Type="text" name="uname"></td>
</tr>

<tr>
<td>enter password</td>
<td><Input Type="password" name="pwd"></td>
</tr>
<tr>

<td><Input type="button" value="tick">remember my password</td>
</tr>


<tr>
<td><Input type="submit" value="submit"></td>
</tr>

<tr>
<td><Input type="reset" value="clear"></td>
</tr>

</Table>
</form>
</center>
</body>
</html>
View Answers

August 2, 2011 at 5:00 PM

1)login.html:

<h1>Login Form</h1>
<form action="http://localhost:8080/examples/validservlet">
<Table>
<tr>
<td>enter name</td>
<td><Input Type="text" name="uname"></td>
</tr>

<tr>
<td>enter password</td>

<td><Input Type="password" name="pwd"></td>
</tr>
<tr>

<td><Input type="checkbox" value="tick">remember my password</td>
</tr>


<tr>
<td><Input type="submit" value="submit"></td>
</tr>

<tr>
<td><Input type="reset" value="clear"></td>
</tr>

</Table>
</form>

2)validservlet.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class validservlet  extends HttpServlet {
  public void doGet(HttpServletRequest request,   HttpServletResponse response)  throws ServletException, IOException {
  PrintWriter pw = response.getWriter();

  String uname=request.getParameter("uname");
String pwd=request.getParameter("pwd");

 String username=getServletConfig().getInitParameter("user");
 String password=getServletConfig().getInitParameter("password");
 System.out.println(username+" "+password);
 if((uname.equals(username))&&(pwd.equals(password))){
     response.sendRedirect("Admin");
 }
 else{
     response.sendRedirect("/examples/jsp/login.html");
 }
  }
}

3)Admin.java:

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

public class Admin  extends HttpServlet {
  public void doGet(HttpServletRequest request,   HttpServletResponse response)  throws ServletException, IOException {
  PrintWriter pw = response.getWriter();
response.setContentType("text/html");
PrintWriter out=response.getWriter();
pw.println("welcome to Admin");
}
}

4)Add following code to web.xml:

<servlet>
    <init-param>
 <param-name>user</param-name>
 <param-value>system</param-value>
 </init-param>
 <init-param>
 <param-name>password</param-name>
 <param-value>rammohan</param-value>
 </init-param>
<servlet-name>validservlet</servlet-name>
        <servlet-class>validservlet</servlet-class>
    </servlet>
<servlet-mapping>
        <servlet-name>validservlet</servlet-name>
        <url-pattern>/validservlet</url-pattern>
    </servlet-mapping>


<servlet>
        <servlet-name>Admin</servlet-name>
        <servlet-class>Admin</servlet-class>
    </servlet>
<servlet-mapping>
        <servlet-name>Admin</servlet-name>
        <url-pattern>/Admin</url-pattern>
    </servlet-mapping>

Ads









Related Tutorials/Questions & Answers:
how to execute the below servlet with html page
how to execute the below servlet with html page  //vallidate user n...,ServletException { res.setContentType("text/html"); PrintWriter out=res.getWriter... res)throws IOException,ServletException { res.setContentType("text/html
how to call servlet from html page at anchor tag ?
how to call servlet from html page at anchor tag ?  I have a very... to other page and at that place i give url of a servlet but in server that url... it is not working properly. I split and merge template. In the header part of my page i use
Advertisements
how to add the calendar to the dynamic rows in html or jsp page - JSP-Servlet
how to add the calendar to the dynamic rows in html or jsp page   Hi Sir, i have 3 columns in my jsp page date of payment,amount recieved,no and i have 2 button in my jsp page ADD and delete button. when i click on add
how to retrieve text and images from mysql database and show on html page using jsp servlet
how to retrieve text and images from mysql database and show on html page using jsp servlet  <%@ page language="java" contentType="text/html... {color:blue} <%@ page import="java.sql.*" %> <%@ page import
How to redirect from a HTML page?
How to redirect from a HTML page?  Hi, Is it possible to redirect from HTML page to another page on the net? I have one page and I want to redirect... to redirect to another page using HTML code. You can use the following code: <meta
find out in servlet which button was clicked on an html page
find out in servlet which button was clicked on an html page  i have two buttons on my jsp page... and i want two call two seperate codes in a servlet on pressing of those 2 buttons.. how do i find out which button was clicked so
how to load values of html form into an excel table using java servlet?
how to load values of html form into an excel table using java servlet?   i have written a java servlet program, which has a html form to be filled. after filling the form the servlet generates a receipt and the values should
Unable to execute JSP page
folder. But I could not execute the JSP page. Please help me...Unable to execute JSP page  I have written one jsp file. It contains html tags and jsp directives. I have saved the file with the extension .jsp
Maven Dependency html-servlet >> 0.0.3
You should include the dependency code given in this page to add Maven Dependency of cat.inspiracio >> html-servlet version0.0.3 in your project
Maven Dependency html-servlet >> 0.0.2
You should include the dependency code given in this page to add Maven Dependency of cat.inspiracio >> html-servlet version0.0.2 in your project
how to include an html page inside a tab
how to include an html page inside a tab  how to include an static html page inside a tab(in jquery)   Please visit the following links...;actually i want to knw how to include an google page inside a tab(suppose in 2nd
How to add two calendars on the same html page
How to add two calendars on the same html page  I have used the same... use single calendar for a single html page.but while implementing two calendars on same html page it doesn't work..The first calendar works but the second
send HTML Email with jsp and servlet
send HTML Email with jsp and servlet  Can You please show me how to send html Email using JSP and Servlet thank you
html and servlet file
html and servlet file  where to place the html and the servlet class... html file with the jsp files amd servlet file insdie classes folder of tomcat. You have to specify the full path of servlet on html action if you want to call
How to pass and catch HTML parameters to a Java program using REST services?(without using servlet/jsp)
replies back to the HTML page as "Login is successful". So my question is how to pass...How to pass and catch HTML parameters to a Java program using REST services... servlet/jsp). Also how to use hibernate criteria? Please help. Its very urgent
which version of eclipse is used to execute Servlet or JSP
which version of eclipse is used to execute Servlet or JSP  Hi Can any body tell me which version of eclipse i have to use inorder to execute Servlet or JSP. previosuly i installed eclipse 3.1 .i am having j2se 5.0 with which i
How to create simple HTML Login Page?
with JavaScript. You will  learn How to create a simple HTML login page... How to make Spring web Login form? HTML Login Page Code...Simple HTML Login Page- Create a Simple HTML Login page and validate
How to create simple HTML Login Page?
with JavaScript. You will  learn How to create a simple HTML login page... How to make Spring web Login form? HTML Login Page Code...Simple HTML Login Page- Create a Simple HTML Login page and validate
Version of cat.inspiracio>html-servlet dependency
List of Version of cat.inspiracio>html-servlet dependency
how to execute jsp and servlets with eclipse
how to execute jsp and servlets with eclipse  hi kindly tell me how to execute jsp or servlets with the help of eclipse with some small program
How to Send particular error to another page in Servlet from web.xml
How to Send particular error to another page in Servlet from web.xml  How to Send particular error to another page in Servlet from web.xml  ...;/location> </error-page> You can also send error from Servlet
How to display Jfreechart from servlet in jsp web page at specified location
How to display Jfreechart from servlet in jsp web page at specified... to display the chart in web page. I generated the chart using Jfreechart in Servlet which is in image format.plz sir give me the code to display this chart
Populate value into HTML page
Populate value into HTML page  Hi, How to populate values from one html page table to another html page..? Thanks
divide html page
divide html page   how to divide html page into 3 parts without using frames
how to execute jsp and servlets with eclipse
how to execute jsp and servlets with eclipse  hi kindly tell me how to execute jsp or servlets with the help of eclipse with some small program.../hibernate/hibernate4/hibernate4ExampleEclipse.shtml Going through this link you may understand how
Include Static HTML Page in JSP
Include Static HTML Page in JSP   ... html page in jsp. In JSP, there are two ways to include another web resource. 1...; tag is used to include html page. The following includeStatic.jsp page shows
how to display jsp page containing mysql query in particular division using ajax ?my code is below bt i cundt get it properly
how to display jsp page containing mysql query in particular division using ajax ?my code is below bt i cundt get it properly   index.html <..." content="text/html; charset=UTF-8"> </head> <script language
html dropdownlist code - JSP-Servlet
html dropdownlist code  hi how to get textfield in html when i select the option others in the dropdown list, pls provide the code?   Hi Friend, Try the following code: function show(){ var op
how to generate a bar chart on a JSP PAGE using the arraylist object passed form the servlet.(using jfreechart)
how to generate a bar chart on a JSP PAGE using the arraylist object passed... it.please reply soon.I am attaching my servlet and jsp page beneath. please reply...; <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML
Hi how to transfer table data from html page to excel sheet by using javascript .
Hi how to transfer table data from html page to excel sheet by using javascript .  html page to excel sheet by using javascript and i dont want to transfer all rows in table, i want to hide some rows in excel sheet. Please send
How to make a servlet to automatically call another servlet at some time instant.
to display in separate HTML page, in which first servlet display should call second...How to make a servlet to automatically call another servlet at some time instant.  Can some one assist me How to make a servlet to automatically call

Ads