nagarjuna
java servlet
2 Answer(s)      6 years and 9 months ago
Posted in : Java Beginners
Hi Friends,

I want to create a small web application using net Beans.My intention is to create a log in page,if the person doesnt have username , password then he use to create a username.For this i want to store these details in a database using MySql,how can i connect the web application with database.
How can i do this.

Ads

View Answers

May 10, 2010 at 3:25 PM


Hi Friend,

Try the following code:

1)login.html:
<html>
<head>
</head>
<body>
<form name="form" method="post" action="http://localhost:8080/examples/jsp/check.jsp">;
<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>
<td align="right"><a href="register.html">New User?</a></td>
</tr>
</table>
</table>
</form>

</body>
</html>

2)check.jsp:

<[email protected] import="java.sql.*"%>
<html>
<body>
<%
String user=request.getParameter("username");
String pass=request.getParameter("password");
%>
<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root";, "root");
Statement st=connection.createStatement();
ResultSet rs=st.executeQuery("Select * from login where username='"+user+"' and password='"+pass+"'");
String username="";
String pwd="";
while(rs.next()){
username=rs.getString("username");
pwd=rs.getString("password") ;
}
if(user.equals(username)&&pass.equals(pwd)){
%>
<p>Hello <%=user%></p>
<p>Login Successful</p>
<%}
else
{
response.sendRedirect("login.html");
}
}
catch(Exception e)
{}
%>
</body>
</html>


May 10, 2010 at 3:26 PM


continue..

3)register.html:

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
</head>

<body>

<form method="POST" action="http://localhost:8080/examples/Registration">;
<table>
<tr><td>User Id:</td><td><input type="text" name="userId" size="20"></td></tr>
<tr><td>First Name:</td><td><input type="text" name="firstname" size="20"></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="lastname" size="20"></td></tr>
<tr><td>User Name:</td><td><input type="text" name="username" size="20"></td></tr>
<tr><td>Password:</td><td><input type="text" name="password" size="20"></td></tr>
<tr><td>Email: </td><td><input type="text" name="email" size="20"></td></tr>
<tr><td>State: </td><td><input type="text" name="state" size="20"></td></tr>
<tr><td>City: </td><td><input type="text" name="city" size="20"></td></tr>
<tr><td>Country: </td><td> <input type="text" name="country" size="20"></td></tr>
<tr><td><input type="submit" value="Submit" name="B1"></td></tr>
</table>
</form>

</body>

</html>

4)Registration.java:

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Registration extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
String connectionURL = "jdbc:mysql://localhost:3306/test";;
Connection connection=null;
ResultSet rs;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String uId = req.getParameter("userId");
String fname = req.getParameter("firstname");
String lname = req.getParameter("lastname");
String uname = req.getParameter("username");
String pass = req.getParameter("password");
String email = req.getParameter("email");
String state = req.getParameter("state");
String city = req.getParameter("city");
String country = req.getParameter("country");
try{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "root");
String sql = "insert into login(id,firstname,lastname,username,password,email,state,city,country) values (?,?,?,?,?,?,?,?,?)";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1, uId);
pst.setString(2, fname);
pst.setString(3, lname);
pst.setString(4, uname);
pst.setString(5, pass);
pst.setString(6, email);
pst.setString(7, state);
pst.setString(8, city);
pst.setString(9, country);
int numRowsChanged = pst.executeUpdate();
out.println("Hello:");
out.println("'"+fname+"'");
pst.close();
}
catch(Exception e){
out.println(e);
}
}
}

Thanks









Related Tutorials/Questions & Answers:
Java servlet
Java servlet  What is servlet
java (servlet) - JSP-Servlet
java (servlet)  how can i disable back button in brower while using servlet or JSP
Advertisements
Java(Servlet) - Servlet Interview Questions
Java Servlet examples  Java and Servlet programming examples
Java servlet
Java servlet  What is the difference between an applet and a servlet
JAVA SERVLET - JSP-Servlet
JAVA SERVLET Log   Hi, A servlet file which create the log files ,then writing values to the list & log, we should have a thousand separator and no decimal values. This means instead of writing 986300.0 in the Log file
java servlet
java servlet   im using ubuntu 12.04 and ialready installed eclipse but i dont know how to run the servlet programs please give brief procedure to run the servlet program in eclipse
java servlet - JSP-Servlet
java servlet  hi friends, Thanks in advance. Am poornima am created new servlet in eclipse its open but without including any code am getting..., Put servlet-api.jar into the lib folder of apache tomcat server. Thanks
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 servlet - Java Beginners
java servlet  how to use java servlet? and what the purpose of servlet?  Hi Friend, Please visit the following link: http://www.roseindia.net/servlets/ Hope that it will be helpful for you. Thanks
Java Servlet - JSP-Servlet
Java Servlet  3-tier structures of servlets
Java Servlet Problem - JSP-Servlet
Java Servlet Problem  I have a servlet class that implemets the ServletContextAttributeListener interface. I am overriding all three methods, but in the attributeReplaced() method, i want to perform a redirect to another servlet
java servlet programming - JSP-Servlet
java servlet programming  i want 2 insert the date time into the sql column of data type datetime through servlets dynamically .How 2 do dat
JAVA JSP and Servlet - JSP-Servlet
JAVA JSP and Servlet  for sending mail through lan without intenet and also chatting facility for online users using Java JSP & SERVLET code.  Hi Friend, Please visit the following links: http://www.roseindia.net
java - JSP-Servlet
java  why is required to write both jsp and servlet in a application of java
JAVA - JSP-Servlet
Java servlets sessions session tracking  How to track the session in Servlet
java servlet programming - JSP-Servlet
java servlet programming  when i m inserting an image file into the sql table in a column of data type binary using prepared statement ps.setBinaryStream, instead of filename it's displaying
Java Servlet - Servlet Interview Questions
Java Servlet  My Servlet Program is generating Compile time errors. 1)pakage javax.Servlte.Http.* does not exists. and Pakage javax.Servlte does... be that you have not set the classpath of your java-servlet-api.jar in the environment
JSP with java/servlet - JSP-Servlet
(java bean or servlet) to fecth the database. Jsp would get the data from...JSP with java/servlet  Thanks Deepak for your answere to my previous.../servlet-jsp-data-list.shtml Hope that it will be helpful for you. Thanks
java servlet code - Servlet Interview Questions
java servlet code  iam doing a project in java, preparation of on line examination in that how to prepare a question paper using servlet code
Java Servlet
Java Servlet  how to create the login form using servlets and how to validate the username and password?   Please visit the following link: http://www.roseindia.net/jsp/loginbean.shtml
Java script in servlet
Java script in servlet  Hi. How to use javascript inside java servlet? Thanks in advance
java,servlet,jsp - JSP-Servlet
java,servlet,jsp   i am doing a project on online examination system and in that there are problems that-- 1.i want to disable the back,forward and refresh button.Please give me the code for google crome browser. 2.suppose
Java servlet - Java Beginners
Java servlet  Hello sir, Please send simple Java servlet coding...:\\ folder.Put servlet-api.jar in in the lib folder of your apche-tomcat.Then go...:\apache-tomcat-6.0.16; 2) Classpath=C:\apache-tomcat-6.0.16\lib\servlet-api.jar
java - JSP-Servlet
Java Servlet Config means   What is Servlet Config means ?  ...://www.roseindia.net/servlets/ServletContext.shtml  Servlet Config means configurion of servlet it is used to cofigure any properties in web.xml and Context
Java servlet - Java Beginners
Java servlet  Thank you for your kind reply. please send Servlet with JDBC connection code  Hi Friend, We are providing you a code that will connect the servlet to database and insert the data. import
java servlet help neeed....
java servlet help neeed....  i have little bit miss concept on servlet and jsp....what ever i know always the jsp provoke first then servlet ...like... the servlet will be called (mapping i have to make in web.xml)......but to some one told
java charts - JSP-Servlet
java charts  Hi,can any one tell me how to create dyanamic charts wrt database contents by using jsp-servlet
Java Servlet - Development process
Java Servlet  Hello Sir how to set Classpath for Java Servlet.  Hi Friend, Put servlet-api.jar file into the lib folder of apache...:\apache-tomcat-6.0.16\lib\servlet-api.jar For more information, visit
Java Problem - JSP-Servlet
Java Problem  What are the steps to run a java Servlet program.with examples  Hi Friend, Please visit the following link: http://www.roseindia.net/servlets/index.shtml Thanks
Java - JSP-Servlet
cmd + (Enter Key) 2. Change the directory to the Servlet file(i.e .java) and 3...Java  What a the steps to compile a Servlet Programe.  Hi... to configure tomcat for running servlet. http://www.roseindia.net/servlets
java servlet compilation error - Servlet Interview Questions
java servlet compilation error  i done the Restart your tomcat server and run your servlet.but i got the problam