0% found this document useful (0 votes)
5 views49 pages

Web development file

The document outlines a series of web development experiments conducted by a B. Tech student, detailing the installation of Java Development Kit (JDK), Eclipse IDE, and Apache Tomcat on Windows. It includes step-by-step instructions for creating Java programs and web pages using HTML and iframes. Each experiment aims to enhance practical skills in web development through hands-on tasks.

Uploaded by

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

Web development file

The document outlines a series of web development experiments conducted by a B. Tech student, detailing the installation of Java Development Kit (JDK), Eclipse IDE, and Apache Tomcat on Windows. It includes step-by-step instructions for creating Java programs and web pages using HTML and iframes. Each experiment aims to enhance practical skills in web development through hands-on tasks.

Uploaded by

Hiya Paliwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

College of Technology and Engineering, MPUAT, Udaipur (Raj.

)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Index

Experiment:1 2

Experiment : 2 21

Experiment-3 24

Experiment-4 27

Experiment-5 30

Experiment-6 38

Experiment 7 42

Experiment 8 46
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Experiment:1
Aim – Download and Install tools for Web Development Step by Step with Their Screenshots.

Java "JDK" or "JRE":-


JRE (Java Runtime), which include a Java Virtual Machine and core libraries, is needed
for running Java programs. JDK (Java Development Kit), which includes JRE plus the
development tools (such as compiler and debugger), is need for writing as well
as running Java programs. In other words, JRE is a subset of JDK. Since you are supposed to
write Java Programs instead of merely running Java programs, you should install JDK,
which includes JRE.

Step By Step Installation:-

Step 1: Download and Install Java Development Kit (JDK)

The very first step is to download the Oracle Java Development Kit (JDK) from the
Official Oracle Website. For that, Head over to the Official Website. .

Here, you can download zip archive, EXE installer or MSI installer. I recommend you use
EXE installer that updates environment variable automatically, which makes the installation
simple and easy.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

So click the download link next to x64 Installer (1). It will download the EXE installer for
Oracle JDK 21 (file name: jdk-21_windows-x64_bin.exe).
Before opening the setup program, it’s strongly recommended to verify integrity of the
downloaded file by checking its SHA256 checksum value. Open a new Command prompt
window and change the current directory to where the file is downloaded. Type the
following command:
certutil -hashfile jdk-21_windows-x64_bin.exe sha256
Then compare the SHA256 checksum value printed by this command against the value
published on the download page (click link 2 as shown in the above screenshot). If both
values are identical, you can proceed next.

Step 2: Install Oracle JDK 21 on Windows 10

Once you have downloaded the Java JDK 21 Software (jdk-21_windows-x64_bin.exe),


double-click on it to see the start of the installation process. Now click on the Next button to
proceed.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Now click Next to see the below image, Here you have a choice to change the Java
installation directory by clicking on the change… button otherwise simply go with the
default path by clicking the Next > button. I am going here with the default path only.

Once you click the Next button, you will see the start of the installation process as below
image. After successful installation, you close the installation wizard.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Step 3: Setup System Environment Variables

Open the system environment variables dialog to do the setup. To open the dialog, you can
type ‘System environment‘ in the Window’s search box as below image and open it.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Once it’s open you can able to see the below dialog box.

Now click on the Environment Variables button > add JAVA_HOME & edit Path as shown
in the below images:-
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Now we should have to add Java Path. If the Path variable is already there then
simply we can click on the Edit… button > then click the New button to add the
path as below image.

Step-4: Verify the Java Installation


To verify the installation open the command prompt by searching it in Window Search and
open it as below image.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Once open the Command Prompt window, type java -version and press Enter key.

Click Next twice to perform the setup with default installation directory
at C:\Program Files\Java\jdk-21. Note that the installer will update the system
environment variable PATH automatically, i.e. putting C:\Program
Files\Common Files\Oracle\Java\javapath as the first entry in the PATH system
environment variable.
Now, open another Command Prompt window and type java -version and javac -
version to verify the installation. It will print the version of Java virtual machine
(java.exe) and Java compiler (javac.exe):

Congratulations! I have successfully installed the Java Development Kit (JDK) 21 on


your Windows 10 operating system.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

How to Download and Install Eclipse R [version 2023-12]:-

Steps to Eclipse software download on Windows:-


Now let’s look at step by step process of installing Eclipse IDE on Windows:

Step1: In the first step, Open your browser and navigate to this
@https://www.eclipse.org/downloads/.

Step 2: Then, click on the “Download” button to download Eclipse IDE.


College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Step 3: Now, click on the “Download x86_64” button.

Step 4: Then click on the “Download” button. After clicking on the download button the
.exe file for the eclipse will be downloaded.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Step 5: Now go to File Explorer and click on “Downloads” after that click on
the “eclipse-inst-jre-win64.exe” file for installing Eclipse IDE .

Step 6: Then, click on “Eclipse IDE for Java Developers”.


College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Step 7: Then, click on the “Install” button.

Step 8: Now click on “Create a new Java project”.


College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Now, you are ready to make new Java projects using eclipse IDE and the screen will look
like this :-

Congratulations! you have successfully Eclipse software download for your


Windows 10 PC.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Installing Apache Tomcat on Windows

Step 1: We need to first install the Tomcat 10 zip file from this website. On the website,
select the 64-bit Windows zip (PGP, sha512) in the Core section to start the download
process for the Tomcat zip file.

Step 2: Check If JDK Is Installed. Open Command Prompt and enter the following
commands:- java –version , javac –version.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

If JDK is not installed on your computer, install JDK. In the above picture, we can see JDK
is already installed on our computer.

Step 3: Unzip Tomcat 10 Zip File. Go to the location where you have downloaded the
Tomcat 10 zip file. Right-click on the apache tomcat file place the cursor on 7-Zip and click
on Extract Here to extract the folder.

Step 4: Creating JAVA_HOME Variable:- Click Start then in the search bar, search for
“Edit the system environment variables” and click on it. The following System Properties
box will open. Select Environment Variables in the box.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

On clicking Environment Variables the following box will open.

Step 5: We have to create a JAVA_HOME variable and insert the path of the JDK file
stored in our computer, which will be shown below. So select New from the System
variables in the above picture. A New System Variable box will be opened where we will
have to fill in the Variable name and Variable value.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Step 6: Go to the location where you have stored the contents of the JDK file in My
Computer or PC. Copy the root path of the location of the JDK file as shown below.

Step 7: Paste the JDK path from the above picture into the Variable value field and in the
Variable name field, give the name JAVA_HOME as shown below.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Step 8: Check the Working of Tomcat. Open the extracted apache tomcat file. We will see
all the following files in them. Among them open the batch file named “startup”.

Step 9: Check if the Startup Windows Batch File is running properly.

By default, the server port number is 8080.


College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Step 10: Successful Installation of Tomcat. Our next step is to open an internet browser
like Google and type in the address

http://localhost:8080
Then click enter and the following browser will appear suggesting the successful installation
of Apache Tomcat.

Congratulations! You have successfully Apache Tomcat Web Server


download for your Windows 10 PC.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Experiment : 2

Aim: Create a Dynamic Web Project and Create a new Html file and write “Hello World”.

Steps to Implement Java Program


Implementation of a Java application program involves the following step. They include:
1. Creating the program
2. Compiling the program
3. Running the program

Step 1. Creating Programs in Java


= We can create a program using Text Editor (Notepad) or IDE (NetBeans).
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Step 2. Compiling the Program in Java


To compile the program, we must run the Java compiler (javac), with the name of the source
file on the “command prompt” like as follows
If everything is OK, the “javac” compiler creates a file called “Test.class” containing the
byte code of the program.
Step 3. Running the Program in Java
We need to use the Java Interpreter to run a program. Java is easy to learn, and its syntax is
simple and easy to understand. It is based on C++ (so easier for programmers who know
C++).
The process of Java programming can be simplified in three steps:
 Create the program by typing it into a text editor and saving it to a file – HelloWorld.java.
 Compile it by typing “javac HelloWorld.java” in the terminal window.
 Execute (or run) it by typing “java HelloWorld” in the terminal window.

The below-given program is the most simple program of Java printing “Hello World” to the
screen. Let us try to understand every bit of code step by step.

Explanation of the above syntax:-

 public: So that JVM can execute the method from anywhere.


 static: The main method is to be called without an object. The modifiers are public and
static can be written in either order.
 void: The main method doesn’t return anything.
 main(): Name configured in the JVM. The main method must be inside the class
definition. The compiler executes the codes starting always from the main function.
 String[]: The main method accepts a single argument, i.e., an array of elements of type
String.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Result: Successfully printed hello world


College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Experiment-3
Aim: Divide page in two rows and dividing second row into two columns and also use iframe tag.

Steps to achieve this aim


Create html file with following divisions

Webpage created will look like:

Iframe tag used as an inline frame (iframe) is a HTML element that loads another HTML page within the
document. It essentially puts another webpage within the parent page.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Before click

After Click

Code used for iframe


College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Division that target the iframe was

Result: Successfully divided page into required divisions and used iframes.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Experiment-4
Aim: Installing Struts 2
Software Needed:
1. Java JDK
2. Struts 2 API
3. Apache Tomcat 9
Steps to follow to download struts 2

1. Download Struts 2 API from struts.apache.org


College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

2. Download struts-2.5.30-all.zip

3. Unzip the zip file using 7zip or WinRAR


4. Download Apache Tomcat Version 9 from tomcat.apache.org
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

5. Download it by clicking on encircled pointer link

6. Further Installation is similar to Tomcat version 10.1 that was installed earlier.

Result: Successfully Installed Struts 2 required softwares!!


College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Experiment-5
Aim: Creating Struts2 Application in eclipse.
Steps to follow to create struts 2 application:
1. Create a Dynamic Web Project and select the target runtime as Apache Tomcat v9.0, leave the
dynamic web module version as 4.0, and in Web Module setting check the option Generate web.xml
deployment descriptor.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

2. Right-click Java Resources -> New -> Source Folder. In the Project Name select the project created
in step 1 and give the Folder Name as src/main/resources.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

3. Right-click resources folder created in step 2 -> New -> Other -> XML File and assign the name of

the file as struts.xml.


4. Copy the given code into struts.xml file.
5. Open web.xml file available at the location src -> main -> webapp -> WEB-INF.
6. Copy the given code into web.xml file.
7. Right-click project -> Properties -> Java Build Path -> Libraries click on Classpath -> Add Library -
> Select User Library -> User Libraries - New - Type the name Struts2 and click OK - Select Struts2
and click Add External JARs.
8. Add the following JAR files available in the Struts API and click OK and then click APPLY and CLOSE:

 commons-fileupload-1.4.jar
 commons-io-2.6.jar
 commons-lang3-3.8.1.jar
 commons-logging-1.2.jar
 freemarker-2.3.31.jar
 javassist-3.20.0-GA.jar
 ognl-3.1.29.jar
 struts2-core–2.5.30.jar
 log4j-api–2.12.4jar
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

9. Right Click project -> Properties -> Select Deployment Assembly -> Add -> Select Java Build Path
Entries -> Next -> Select Struts2 -> Finish -> Apply -> Apply and Close
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

10. Right Click project -> New -> JSP File, create a file named success.jsp and copy the given code into
it
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

11. Similarly, create a file named error.jsp and copy the given code into it

12. Right Click project -> New -> Class, give the class name as MyAction and click OK

13. Copy the given code into MyAction.java file"


14. Struts 2 Application in Eclipse Configuration and Execution
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Creating Struts 2 App in Eclipse …


15. Click on project -> Run -> Run on Server -> Select Tomcat v9.0 Server and click on Finish
Even though you might see a HTTP 404 error page

, at the end of the URL include getMessage.action and press Enter key
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

You will see the success.jsp page as output

Result: Successfully implemented struts-2 application.


College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Experiment-6
Aim: To install MySQL database.
Follow the following steps to install MySQL database server:
1. Go to https://dev.mysql.com/downloads/file/?id=526408 and No thanks, just start my download.

2. Select the mysql-installer-community-8.0.36.0.msi and hit enter following screen will appear
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

3. Choose “Custom” as Setup Type and click “next”

4. Select products as shown below:


College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

5. Click on execute to download the required product:

6. Click on next until you come across accounts and roles screen:

7. Enter any password for MySQL server and click next until you come across Apply Configuration.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

8. Click on execute when following screen appears

9. After successful configuration click on Finish.

Result: Successfully Installed MySQL database!!


College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Experiment 7
Aim: Write a program for Struts and MySQL connectivity.

Tools Used:
1. Eclipse
2. Struts 2.5.33
3. Tomcat Server v9.0
4. MYSQL Database v8.3.0

Step-1: To create a MySql Database :


1. Open Command prompt or Terminal .
2. Write command myql -u root -p .
3. To create database : create database struts2; .
4. Then to use this database : use struts2;
5. To create table : create table login(user varchar(50) primary key , password varchar(50) , name
varchar(50)); .

Step-2: Open Eclipse , create a dynamic web project and configure it with struts2 .
1. Upload all the jar files .
2. Create struts.xml , web.xml , Myaction.java files .
3. Download and upload mysql-connector-j-8.3.0.jar file , used to connect struts2 and MySql database .

Step-3: Configure MyAction.java , struts.xml , web.xml files.

MyAction.java:
import com.opensymphony.xwork2.ActionSupport;
import java.sql.*; @SuppressWarnings("serial") public class MyAction
extends ActionSupport {
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

public String execute() { String ret =


ERROR;
Connection con = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con =
DriverManager.getConnection("jdbc:mysql://localhost:8080/struts2","root ","sajalsinghal")
}

Struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration
2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" namespace="/" extends="strutsdefault">
<action name="abc" class="MyAction">
<result name="success">/success.jsp</result>
<result name="failure">/error.jsp</result>
</action>
</package>
</struts>

Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

app_5_0.xsd" id="WebApp_ID" version="5.0">


<display-name>StrutsDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file> <welcome-file>default.htm</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-
class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndE xecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

After performing the above steps, we have successfully connected our MySQL database with Struts2
Application.
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Experiment 8
Aim: Write a program for using MySQL database to store data from “FORM” made using JSP.

Tools Used:
1. Eclipse
2. Struts 2.5.33
3. Tomcat Server v9.0
4. MYSQL Database v8.3.0

Code –
register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="register" method="post">
Username: <input type="text" name="username"><br>
Email: <input type="email" name="email"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Register">
</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Registration Successful!</h2>
<p>Congratulations! Your registration was successful.</p>
<p><a href="register.jsp">Go back to registration page</a></p>
</body>
</html>
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Registration Error</h2>
<h2>Error</h2>
<p>An error occurred: <%= request.getAttribute("errorMessage") %></p>
<p>Please try again later.</p>
<p><a href="register.jsp">Go back to registration page</a></p>
</body>
</html>

register.java

package in.sp.backend;

import java.io.IOException; import java.io.PrintWriter; import


java.sql.Connection; import java.sql.DriverManager; import
java.sql.PreparedStatement; import java.sql.SQLException; 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 Register extends HttpServlet {
private static final String URL = "jdbc:mysql://localhost:3306/register"; private static final String USER =
"root"; private static final String PASSWORD = "root";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();

String username = request.getParameter("username");


String email = request.getParameter("email");
String password = request.getParameter("password");
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

try {
// Load the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Establish a connection to the database
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
// Prepare SQL statement for inserting user data
String sql = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql); statement.setString(1,
username); statement.setString(2, email); statement.setString(3, password);

// Execute the SQL statement to insert user data statement.executeUpdate();

// Close the database connection conn.close();

// Redirect the user to the success page response.sendRedirect("success.jsp");


} catch (ClassNotFoundException | SQLException e) {

request.setAttribute("errorMessage", e.getMessage());
request.getRequestDispatcher("error.jsp").forward(request, response); }
}
}

Database register and Table users

CREATE DATABASE register;

USE register; CREATE TABLE users ( id INT

AUTO_INCREMENT PRIMARY KEY, username

VARCHAR(50), email VARCHAR(50), password

VARCHAR(50)

);
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name –Raman Raj Singh Class - B. Tech, III Year Branch - AI & DS Semester – VI
Subject – Web Development (AI 367)

Output –

Result: Successfully implemented a program for using MySQL database to


store data from “FORM” made using JSP!!

You might also like