write a program in servlet to take username & password from html file & print the appropriate message to user
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> </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 sevletjava 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 dropdownjsp -
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
java servlet - Servlet Interview Questionsjava 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-ServletJava 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
username and password in servletusername and password in servlet i'm usng eclipse luna(
java programming)
Sevlet and apache tomcat8.0
i need to do a login page
then after login it must display either invalid user
or valid user
here is what i done for login
Cart QuantityCart Quantity How to increase and decrease quantity from a cart using session in jsp and
sevlet MVC architecture example - Java Beginners")
2.Then create a servlet page name like login.java
3.Then create another
java class Login.java(It the same name of the
sevlet)where get and set mathod will be used.
4.Then create another
java class where you will put your database
JSP FUNDAMENTALS;
JSP termed as
Java Server Pages is a technology introduced by Sun...=?
Java? extends=?<Class name>?
import=?<class> or <package>...:
a.
Language = ?
Java?
b.  
servlets - Strutsservlets Hi,
i am deployed one
sevlet in tomcat,and another servlet is deploye in weblogic .then how i need to cantact the servlet in web logic by the servlet in tomcat. See this site for the answer:
http
javajava diff bt core
java and
java JAVAJAVA how the name came for
java language as "
JAVA java java why iterator in
java if we for loop
javajava explain technologies are used in
java now days and structure
java javajava different between
java & core
java Java Java Whether
Java is pure object oriented Language
javajava is
java open source
javajava what is
java reflection
java java in
java does not pointers concept but what is nullpointers in
java?
nullpointer is a runtime Exception
javawhat is the size of array in
java ? what is the size of array in
java ?
what is the mean of finalize in
java javajava give a simple example for inheritance in
java javajava why to set classpath in
java javajava why to set classpath in
java javajava RARP implementation using
java socket
javajava sample code for RARP using
java javajava Does
java allows multiline comments
JavaJava how to do
java in command prompt
javajava how use
java method
javajava what are
JAVA applications development tools
JavaJava Whether
Java is Programming Language or it is SOftware
javajava is
java purely object oriented language
javajava why multiple inheritance is not possible in
java javajava explain object oriented concept in
java java java difference between class and interface
JavaJava how to draw class diagrams in
java javajava give a simple example for inheritance in
java javajava why to set classpath in
java javajava why to set classpath in
java java java What is ?static? keyword
javajava Write a
java code to print "ABABBABCABABBA
javajava write a program in
java to acess the email
javajava send me
java interview questions
javajava write a
java program using filenotfoundexception
java java How to set
java Policy for applet using jdk 6
javajava pattern code for a given words
java pattern code for a given words pattern