[Go to site: main page, start]

0% found this document useful (0 votes)
15 views14 pages

Java Servlet Login and Registration Guide

The document provides a comprehensive guide on creating a login form using Java Servlets and MySQL, including HTML forms, servlet code for handling login and registration, and database interactions. It includes examples of login, registration, and welcome pages, along with the necessary Java code for each servlet. Additionally, it outlines the configuration needed in web.xml for servlet mapping.

Uploaded by

lachampapa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

Java Servlet Login and Registration Guide

The document provides a comprehensive guide on creating a login form using Java Servlets and MySQL, including HTML forms, servlet code for handling login and registration, and database interactions. It includes examples of login, registration, and welcome pages, along with the necessary Java code for each servlet. Additionally, it outlines the configuration needed in web.xml for servlet mapping.

Uploaded by

lachampapa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Login Form using JAVA Servlet with MySQL Database

[Link]
<html>
<head>
<title>TODO supply a title</title>
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
</head>
<body>
<center><form action="LoginServlet" method="post">
<table>
<tr><th>UserName:</th><th> <input type="text"
name="username"/></th> </tr>
<tr><th>Password:</th><th><input type="password"
name="password"/></th> </tr>
<tr><th></th><th><input type ="submit" value =
"login"/></th> </tr>
</table>
</form>
</center>
</body>
</html>
2) [Link]
<html>
<head>
<title>TODO supply a title</title>
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
</head>
<body>
<center>Home Page ( Login successfully )</center>
</body>
</html>
3) [Link]
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet(urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {
@Override
Protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
[Link]("text/html");
PrintWriter out = [Link]();
String username = [Link]("username");
String password = [Link]("password");
try {
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/mydb","root", "root");
Statement stm = [Link]();
ResultSet rs = [Link](
"select * from login where username='" + username + "' and
password='" + password + "'");
if ([Link]())
{
[Link]("[Link]");
}
else {
[Link]("<center>Invalid Username or
Password</center>");
}
[Link]();
} catch (Exception e) {
[Link]([Link]());
}
}
}
2. Login form using servlets
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>
<h1>Sign Up Form</h1>
<form method="get" action=" InsertDataServ ">
Enter Username:<input type="text" name="txtuname"/><br/>
Enter Password:<input type="password"
name="txtupass"/><br/>
<input type="submit" value="Sign Up"/>
<input type="reset" value="Reset"/>
</form>
</body>
</html>
2. [Link]
package servlet;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
public class InsertDataServ extends HttpServlet
{
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
PrintWriter out = [Link]();
String n = [Link]("txtuname");
String p = [Link]("txtupass");
try {
[Link]("[Link]");
Connection con = [Link]
("jdbc:mysql://localhost/user","root", root");
Statement stmt = [Link]();
[Link]("insert into usertab values('"+n+"',
'"+p+"')");
[Link]("<h1>Your account is created successfully!!!</h1>");
}
catch (Exception e)
{
[Link]("Sorry!!! Try again later.");
}
}}

3. Registration form using servlet with jdbc


1)[Link]
<html>
<head>
<title>home</title>
</head>
<body>
<form name="f1" method="post" action="Registration">
<table>
<tr>
<td><label>Register Number:</label></td>
<td><input type="text" name="rn"></td>
</tr>
<tr>
<td><label>First Name:</label></td>
<td><input type="text" name="fn"></td>
</tr>
<tr>
<td><label>Last Name:</label></td>
<td><input type="text" name="ln"></td>
</tr>
<tr>
<td><label>Department:</label></td>
<td><input type="text" name="dept"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Register"></td>
</tr>
</table>
</form>
</body>
</html>
2) [Link]
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class Registration extends HttpServlet {
public void doPost (HttpServletRequest request,
HttpServletResponse response) throws IOException {
[Link]("text/html");
PrintWriter pw = [Link]();
String regno = [Link]("rn");
String fn = [Link]("fn");
String ln = [Link]("ln");
String dept = [Link]("dept");
try {
[Link]("[Link]");
Connection con =
[Link]("jdbc:derby://localhost:1527/java
", "root", "root");
PreparedStatement ps = [Link](
"INSERT INTO student VALUES(?, ?, ?, ?)");

[Link](1, [Link](regno));
[Link](2, fn);
[Link](3, ln);
[Link](4, dept);
int i = [Link]();
[Link](i + " Record Inserted Successfully");
[Link]();
}
catch (Exception e) {
[Link](e);
}
}
}
3) [Link]
<web-app>
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>Registration</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/Registration</url-pattern>
</servlet-mapping>
</web-app>
4. To display “welcome” usimg servlet
[Link]
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h2>Welcome to My Servlet Example</h2>
<form action="welcome" method="get">
Enter your name: <input type="text" name="username">
<input type="submit" value="Submit">
</form>
</body>
</html>

2) [Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Welcome extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)throws ServletException,
IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet Welcome</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<h1>Servlet Welcome at " +
[Link]() + "</h1>");
[Link]("<h2>Hello, Welcome to the Servlet
Example!</h2>");
[Link]("</body>");
[Link]("</html>");
}
}

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException,
IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)throws ServletException,
IOException {
processRequest(request, response);
}

@Override
public String getServletInfo() {
return "Welcome Servlet Example";
}

3) [Link]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
version="3.1">
<!-- Welcome Servlet -->
<servlet>
<servlet-name>first</servlet-name>
<servlet-class>Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>first</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>

<!-- Optional: Another Servlet -->


<servlet>
<servlet-name>NewServlet</servlet-name>
<servlet-class>NewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewServlet</servlet-name>
<url-pattern>/NewServlet</url-pattern>
</servlet-mapping>
</web-app>

You might also like