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

 
 


rammohan
how to execute the below servlet with html page
1 Answer(s)      a year and 10 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>
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 Pages:
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
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 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
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 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
html
html  how to convert html page into jsp page
HTML
HTML  How do we specify page breaks in HTML
Include Static HTML Page in JSP
Include Static HTML Page in JSP       This example shows how to include static html page... to include html page. The following includeStatic.jsp page shows the include tag
html
html  Can we Access database through html page and how,if not then why
html
html  how we can give the form actions in html after register button in the login page
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
html-jsp
html-jsp  If i want to get dynamic value in html textbox or in jsp,then how can I get the value,how the value will be transfered from servlet page to the html textbox.Thanx in advance.....Kindly help me
divide html page
divide html page   how to divide html page into 3 parts without using frames
html
html  How to develope a web page with Audio and vidios? give One example
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
HTML
HTML  How can we increase the row size in the grid form view of the table.I want to store the maximum 4 details of the candidate in one page of the grid table view
Html
Html  can we send data from one html page to another html page? if yes how?   Yes, using javascript. 1)page1.html: <html> <form type=get action="page2.html"> <table> <tr> <td>First Name
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
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-jsp - getparameter() - JSP-Servlet
html-jsp - getparameter()  Hi! friedns, This is my front end... getParameter() with two form action as shown below. But I am not getting the parameters in the second page it is showing null, null. kindly help me to write
Page object - JSP-Servlet
Page object  Hello friends, How can we make use of PAGE object of implicit JSP object. If this is possible explain me about... for more information. http://www.roseindia.net/jsp/simple-jsp-example/Html-Tag
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
Servlet refresh page
Servlet refresh page In this example we will show you how you can refresh your page from setting the refresh attribute from a servlet. response.setHeader... refresh code into HTML page. Read more at http://www.roseindia.net/software
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 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
create html of word ,excel,ppt - JSP-Servlet
create html of word ,excel,ppt  Plzz tell me how to create html page of word , excel ,ppt document dynamically using jsp because in my project I have to store uploaded files as html page
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
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 Map the First_Servlet to Second_Servlet - JSP-Servlet
Html page map with first_servlet.and first_servlet forward the response to Jsp file. In Jsp have a function which define in second_servlet. How that fuction...How Map the First_Servlet to Second_Servlet  My Query: Hello Sir, I
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
HTML
in HTML. In this Tutorial, the code create a HTML Page, which depicts you how... The Tutorial illustrates an example, how to post image in HTML page. In this Tutorial, the code describe you html page that shows you how to insert images
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 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 Store HTML img Into The MySQL Using Java
How To Store HTML img Into The MySQL Using Java In this section you will read about how to get the image from Html img src tag and saved... page named image.jsp where I have used the jquery for getting the Html img
Show Hyperlink in HTML Page
Show Hyperlink in HTML Page   ... code show you a HTML page, that create a link to another page. The code begins...; On execution of html code, the page is displayed as  The web
pl z provide code for below question in javascripr or jsp - JSP-Servlet
pl z provide code for below question in javascripr or jsp  Once the parallel request happened, the user will not be able to continue his work, until the first request ends by itself and back to the home page   Hi
servlet
servlet  how to jsp integer are type cast int servlet page   Hi Friend, Try the following code: 1)form.jsp: <form method="post...) throws ServletException,IOException { response.setContentType("text/html
html form - JSP-Servlet
html form  how to retrieve database value in dropdown list box placed in html form  Hi friend, Visit for more information. http://www.roseindia.net/jsp/ Thanks
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
Execute database query by using sql tag of JSTL SQL library
how to execute sql query. To execute sql query we have used a sql tag <sql...Execute database query by using sql tag of JSTL SQL library... to create application that execute sql query given by user using JSTL SQL Library
JSP:HTML Form in-place Editing - JSP-Servlet
JSP:HTML Form in-place Editing  Hi, I want to thank the people... developer's needs. This is a follow up to the HTML Form in-place editing. The code... to be able to open a page instead of a window for the editing which opens
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
PDO Fetch Execute
PDO Fetch Mode: We have studied before in the earlier tutorials that how to connect with a database and how to fetch data from the tables, in many times we... array. Example:strong> <?php header('Content-type:text/html'
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
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 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
How to use 'if' statement in jsp page?
How to use 'if' statement in jsp page?  ... how to use 'if' statement in jsp page. This statement is used to test..._if_statement.jsp <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.