java sevlet

java sevlet

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

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
sevlet - JSP-Servlet
JBoss and Sevlet - JSP-Servlet
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
username and password in servlet
username 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 Quantity
Cart 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 - Struts
servlets  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
Deployment on Server that can be used simultaneously by more than one user
Deployment on Server that can be used simultaneously by more than one user  Sir, I have deployed my web application developed using JSP & sevlet. how could i access it on the network using xp os. I have deployed it on Tomcat
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  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  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  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  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   What is ?static? keyword
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  write a java program using filenotfoundexception
java
java  hi im new to java plz suggest me how to master java[email protected]
java
java   How to set java Policy for applet using jdk 6
java
java pattern code for a given words  java pattern code for a given words pattern

Ads