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




Have Programming Question? Ask it here!
 
 
 


deepika s m
java sevlet
1 Answer(s)      3 years and 5 months ago
Posted in : JSP-Servlet


write a program in servlet to take username & password from html file & print the appropriate message to user


Advertisement
View Answers

November 12, 2010 at 4:18 PM


Hi Friend,

Try the following code:

1)login.html:

<html>
<head>
</head>
<body>
<form name="form" method="post" action="http://localhost:8080/examples/login">
<br><br>
<table align="center"><tr><td><h2>Login Authentication</h2></td></tr></table>
<table width="300px" align="center" style="border:1px solid #000000;background-color:#efefef;">
<tr><td colspan=2></td></tr>
<tr><td colspan=2>&nbsp;</td></tr>
  <tr>
    <td><b>Login Name</b></td>
    <td><input type="text" name="username" value=""></td>
  </tr>
  <tr>
    <td><b>Password</b></td>
    <td><input type="password" name="password" value=""></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="submit" name="Submit" value="Submit"></td>
  </tr>
 <tr>
 <td></td>
 </tr>
</table>
</table>
</form>
</body>
</html>

2)login.java:

import java.io.*;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class login extends HttpServlet {

public  void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String userid = request.getParameter("username");
String password = request.getParameter("password");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root");

String query = "SELECT * FROM user_details WHERE username = '"+userid+"' and password = '"+password+"'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
String username="";
String pwd="";
while(rs.next()){
 username=rs.getString("username");
 pwd=rs.getString("password") ;
}
if((username.equals(userid))&&(pwd.equals(password))){
out.println("<html>");
out.println("<head><title>Login Success</title></head>");
out.println("<body>");
out.println("<p>Login Success! Welcome " + userid);
out.println("</body></html>");
} 
else{
out.println("<html>");
out.println("<head><title>No Such User</title></head>");
out.println("<body>");
out.println("<p>Login Failed! ");
out.println("<p>No such user exists ");
out.println("</body></html>");
out.println("<a href=\"login.html\">Click here to login</a>");
out.close();
conn.close();
}
} catch (Exception e) {
System.out.println("Error" + e.getMessage());
}

out.close();
}
}

Thanks



Related Tutorials/Questions & Answers:
java sevlet
java sevlet  write a program in servlet to take username & password from html file & print the appropriate message to user
jsp -sevlet connecting to database using dropdown
jsp -sevlet connecting to database using dropdown  How can I get my dropdown list from oracle database and then submit it to another table in JSP. I am thinking of a form that links to a servlet and the servlet connects
Advertisements
jsp or sevlet and html form to send picture to database - JSP-Servlet
jsp or sevlet and html form to send picture to database  Hello guys, thanks for your help. Please help me.. I want to insert student information that contains his picture to database. What I need are html page for the form
java servlet - Servlet Interview Questions
java servlet   i use tomcat server and eclipse as ide for j2ee environment when i tries the example for sevlet context in roseindia to make entry in server log file ( context.log method to log entry) but could not see the log
Java Servlet - JSP-Servlet
Java Servlet   Hello Sir Could you help me in understanding directory Structure for servlet page in eclipse (IDE). I save my Sevlet in WEB-INF but it is not displaying, it is displaying code of servlet. Process used
java
java  diff bt core java and java
java
java  what is java
JAVA
JAVA  how the name came for java language as "JAVA
java
java   why iterator in java if we for loop
java
java  explain technologies are used in java now days and structure java
java
java  different between java & core java
Java
Java   Whether Java is pure object oriented Language
java
java  is java open source
java
java  what is java reflection
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   What is ?static? keyword
java
java  RARP implementation using java socket
java
java  sample code for RARP using java
java
java  Does java allows multiline comments
Java
Java  how to do java in command prompt
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  Write a java code to print "ABABBABCABABBA
java
java  write a program in java to acess the email
java
java  send me java interview questions
java
java  how use java method
java
java  what are JAVA applications development tools
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  is java purely object oriented language
java
java  why multiple inheritance is not possible in java
java
java  explain object oriented concept in java
java
java   difference between class and interface
Java
Java  how to draw class diagrams in java
java
java  write a java program using filenotfoundexception
java
java  hi im new to java plz suggest me how to master java....saifjunaid@gmail.com
java
java   How to set java Policy for applet using jdk 6
java
java  dear, i want a field for date picker using java/java script
java
java  create java program for delete and update the details,without using database, just a normal java program
java
java  why methods in java raise exceptions   Have a look at the following link: Java Exceptions
java
java code to search the nodes  how to write the java code to search the nodes using routers
java
java online telephone directory  i need coding for online telephone directory..by using java....pls help me
java
java  how to edit text document by using java then how to edit starting and ending of text document by using java
java
java  different between java & core java print("code sample
Advertisements
 

 

 

DMCA.com