JSP-Servlets
OJT :
Technologies used :
1) front-end : html, css, js, bootstrap,jsp
2) back-end : java, servlet
3) Database : mysql.
software Used :
1. Eclipse
2. Xampp
html simple tag :
Document Structure Tags
<!DOCTYPE html>
: Declares the document type and version of HTML (HTML5 in this case).<html>
: The root element of an HTML document.<head>
: Contains meta-information about the document (such as its title, character set, styles, scripts, etc.).<body>
: Contains the content of the HTML document, visible to users.
Simple jsp program :
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Example</title>
</head>
<body>
<h1>Welcome to JSP</h1>
<%-- Scriptlet --%>
<%
String message = "Hello, JSP!";
%>
<p>Message: <%= message %></p>
</body>
</html>
1. <nav>
<ul>
<li><a href="home.jsp">Home</a></li>
<li><a href="about.jsp">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
2. <img src="image.jpg" alt="Description of image">
3. <link>
: Links external resources like stylesheets.
<form action="register" method="post">
<jsp:include page="header.jsp" />
3. <a href="BookAppoitement.jsp?id=<%=rs.getString(1)%>&drname=<%=rs.getString(2)%>&disease=<%=disease%>&fee=<%= request.getAttribute("fee") %>">
Book appointment
</a>
3. <form action="Login.java" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<input type="submit" value="Submit">
</form>
Registration Code :
register.jsp
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h2>Registration Form</h2>
<form action="register.java" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<label for="contact">Contact:</label>
<input type="text" id="contact" name="contact" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="address">Address:</label><br>
<textarea id="address" name="address" rows="4" cols="50" required></textarea><br><br>
<input type="submit" value="Register">
</form>
<p>Already have an account? <a href="login.jsp">Login here</a></p>
</body>
</html>
User.java
public class User {
private String username;
private String password;
private String contact;
private String email;
private String address;
public User() {}
public User(String username, String password, String contact, String email, String address) {
this.username = username;
this.password = password;
this.contact = contact;
this.email = email;
this.address = address;
}
// Getters and setters
// (You can generate them automatically in IDEs like IntelliJ IDEA or Eclipse)
// Or manually as shown below:
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
register.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserDatabase userDatabase;
public void init() {
userDatabase = new UserDatabase();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String contact = request.getParameter("contact");
String email = request.getParameter("email");
String address = request.getParameter("address");
User user = new User(username, password, contact, email, address);
if (userDatabase.registerUser(user)) {
response.sendRedirect("login.jsp");
} else {
response.sendRedirect("register.jsp");
}
}
}
Database.java :
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDatabase {
private String jdbcURL = "jdbc:mysql://localhost:3306/userdb";
private String jdbcUsername = "root";
private String jdbcPassword = "password";
protected Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return connection;
}
public boolean registerUser(User user) {
String INSERT_USERS_SQL = "INSERT INTO users (username, password, contact, email, address) VALUES (?, ?, ?, ?, ?)";
boolean result = false;
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) {
preparedStatement.setString(1, user.getUsername());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setString(3, user.getContact());
preparedStatement.setString(4, user.getEmail());
preparedStatement.setString(5, user.getAddress());
result = preparedStatement.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public User validateUser(String username, String password) {
String SELECT_USER_BY_USERNAME = "SELECT username, password, contact, email, address FROM users WHERE username = ?";
User user = null;
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(SELECT_USER_BY_USERNAME)) {
preparedStatement.setString(1, username);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
if (rs.getString("password").equals(password)) {
user = new User();
user.setUsername(username);
user.setPassword(password);
user.setContact(rs.getString("contact"));
user.setEmail(rs.getString("email"));
user.setAddress(rs.getString("address"));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
}
database :
CREATE DATABASE userdb;
USE userdb;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(100) NOT NULL,
contact VARCHAR(15),
email VARCHAR(100),
address VARCHAR(255)
);
hibernate :
basic we should know about :
1. core java - oops
2. JDBC - basic API
3. Database - tables, queries
What is hibernate :
hibernate is java framework that simplifies the development of java application to interact with database.
Hibernate is an object-relational mapping (ORM) tool for the Java programming language.
hibernate invented by Gavin King in 2001.
you can build any type of application in hibernate. like web application, enterprise, swing, springboot.
you can build any type of application in hibernate. like web application, enterprise, swing, springboot.
Comments
Post a Comment