Professional Documents
Culture Documents
Vidul S A
20BIT061
7. Web application development using JSP with JDBC
First, we have to create a table in MySql
create table USER_ACCOUNT
(
USER_NAME VARCHAR(30) not null,
GENDER VARCHAR(1) not null,
PASSWORD VARCHAR(30) not null,
primary key (USER_NAME)
);
-- Create table
create table PRODUCT
(
CODE VARCHAR(20) not null,
NAME VARCHAR(128) not null,
PRICE FLOAT not null,
primary key (CODE)
);
Next we have to add the html file for the front page
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple Web Application</title>
</head>
<body>
<ul>
<li><a href="home">Home</a></li>
<li><a href="login">Login</a></li>
<li><a href="productList">Product List</a>
</ul>
</body>
</html>
Now we have to write the java codes for both the tables
package org.o7planning.simplewebapp.beans;
public UserAccount() {
Product.java
package org.o7planning.simplewebapp.beans;
public Product() {
Database connection
package org.o7planning.simplewebapp.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Class.forName("com.mysql.jdbc.Driver");
User.java
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public User(){}
this.id = id;
this.name = name;
this.profession = profession;
return id;
@XmlElement
this.id = id;
}
public String getName() {
return name;
@XmlElement
this.name = name;
return profession;
@XmlElement
this.profession = profession;
User_Dao.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
try {
if (!file.exists()) {
userList.add(user);
saveUserList(userList);
else{
ois.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
return userList;
try {
FileOutputStream fos;
oos.writeObject(userList);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
UserService.java
package com.tutorialspoint;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/UserService")
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
return userDao.getAllUsers();
}
web.xml
<?xml version = "1.0" encoding = "UTF-8"?>
xmlns = "http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
<display-name>User Management</display-name>
<servlet>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.tutorialspoint</param-value>
</init-param>
</servlet>
<servlet-mapping>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>