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



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


deepika s m
java sevlet
1 Answer(s)      4 years and 2 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   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
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   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
Java
Java  how to draw class diagrams in java
java
java  explain object oriented concept in java
java
java  is java purely object oriented language
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  how use java method
java
java  write a program in java to acess the email
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  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
 

 

 

DMCA.com