Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Servlet-Interview-Questions how to execute the below servlet with html page
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


rammohan
how to execute the below servlet with html page
1 Answer(s)      3 years and 4 months 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>

Advertisement
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>


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 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
Advertisements
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
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
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 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 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 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 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
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 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
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
divide html page
divide html page   how to divide html page into 3 parts without using frames
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
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
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
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
page breaks in HTML
page breaks in HTML  How do I specify page breaks in HTML
page breaks in HTML
page breaks in HTML  How do I specify page breaks in HTML
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 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
simplest HTML page
simplest HTML page  What is the simplest HTML page
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
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 .   
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
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 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
HTML login page code
HTML login page code  Hi all, I am writing my first HTML JavaScrip validation page called "UserLogin.html". So, please guide me how to validate user... the complete HTML JavaScript code for User Login Page. Thanks in Advance!   
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
 

 

 

DMCA.com