Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

how to execute the below servlet with html page

Ads

 
 

Share on Google+Share on Google+

rammohan
how to execute the below servlet with html page
1 Answer(s)      6 years ago
Posted in : Servlet Interview Questions
//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>
Ads
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 execute this code - JSP-Servlet
how to execute this code  hi guys can any help me in executing this bank application http://www.roseindia.net/jsp/bank.shtml, i need to use any database plz tell me step-to-step procedure for executing this,i need to create
Advertisements
How to pass parametes from JSP page to HTML page? - JSP-Servlet
How to pass parametes from JSP page to HTML page?  Hi all, In my project I have one JSP page and one HTML page. In JSP page I have created HTML... with the username in HTML which I have called in JSP page. So I just want
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
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 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
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
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
How to execute shellscript in Javascript
How to execute shellscript in Javascript   Hello Can anyone please explain me how to execute shellscript in Javascript Thanks
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 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
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
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
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 pass form values from javascript of html page to jsp page
how to pass form values from javascript of html page to jsp page   This is my sample html page which contains inline javascript which calculates the geocode and tries to return the lattitude and longitude .But my question is how
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
Calendar window is not coming for the Dynamic rows in html page - JSP-Servlet
Calendar window is not coming for the Dynamic rows in html page  Calendar window is not coming for the Dynamic rows in html page. I have 3 textboxes. one column is having the date textbox .I need to add the calendar icon
Servlet Generates HTML
Servlet Generates HTML  How Servlet Generates HTML ?   Servlet chaining is a technique in which two or more servlets can cooperate in servicing a single request. In servlet chaining, one servlet?s output is the input
Execute PHP in HTML
have seen how to execute php in html page. Now lets find out the reason...Execute PHP in HTML In this tutorial you will learn about the different ways of executing PHP code into HTML pages. In general, we have seen that a page
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 map the first_servlet to second_servlet - JSP-Servlet
How map the first_servlet to second_servlet  Hello, I have two questions. 1.How I want map the second_servlet by first_servlet. My Html page map... have a function which define in second_servlet. How that fuction which define
Html Template - JSP-Servlet
Html Template   I use a template to design my website,but some problems arised using template.I pass flag update and insert as a hidden field on to the servlet,the response of the servlet is a jsp page.on this page template
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
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
Sending form data from HTML page to SQLserver 2005 database by calling servlet code
Sending form data from HTML page to SQLserver 2005 database by calling servlet code  Hi sir, I would like to know how to send the form data from html page to database by calling servlet code from html page .   
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 generate captcha in jsp page ? - JSP-Servlet
how to generate captcha in jsp page ?  hi friends, i would like to implement Captcha in login screen. i'm unsing struts. could you please give some sample codes to implement this. Thank you Ganesh  Hi, This link
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