You are on page 1of 5

Steps for Running Any Servlet Programs

1. Install Java:
jdk-8u361-windows-x64
2. Install Tomcat 9.0
apache-tomcat-9.0.73

3. Install Eclipse
Eclipse IDE for Enterprise Java and Web Developers -
2022-12 (You can install latest version)
4. Configure the Environment to Run the servlet.
1. Add server and start it.

5. Create html and servlet file and write the logic into it.
6. Save the Project and run on server.

Assignment No 6: Implementation

1. Create a Dynamic web project. Give some appropriate


name to project, click on web.xml file, follow all the on-
screen instructions and at the end click on finish button.

2. Right Click on the folder(i.e project just created), and


create an html file. Give some appropriate name to it.

3. Write down script in html file for entering book


information like name , id of book and so on.
4. Open the html file in any of the browser, to check
whether html page is created correctly.

5. Create database in Mysql


a) Download Mysql:mysql-installer-community-8.0.32.0

b) Download : mysql-connector-j-8.0.32

c) Write down the following code in MySql for creating


database
create database bshop;
use bshop;
create table ebook(id int(3),title varchar(40));

6. Right click on the folder (i.e project just created) and


create a servlet file. Give some appropriate name to it.
7.Right Click on project, click on buildpath--
>Click on configure Buildpath-->Add sql jar
file

8. In doGet method write down the logic for Inserting the


record and fetching the data from database.

//Following code is To Insert Record in database:


import javax.servlet.annotation.WebServlet;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {

response.setContentType("text/html");

PrintWriter out=response.getWriter();

String name=request.getParameter("bname");

String id=request.getParameter("bid");

try {

Class.forName("com.mysql.cj.jdbc.Driver");//Mysql
Driver is Registered

//Connection is Establish

Connection
con=DriverManager.getConnection("jdbc:mysql://local
host:3306/bshop","root","root");
PreparedStatement ps=con.prepareStatement("insert
into book values(?,?)");

ps.setString(1,name);

ps.setString(2,id);

int i=ps.executeUpdate();

if(i>0)

out.println("\n Data Stored Successfully in


database\n");

else

//Following code is to fetch Record in database:


PreparedStatement ps1=con.prepareStatement("select
* from book");

ResultSet rst=ps1.executeQuery();

out.println("\n\nName \t\tId <br>");

while(rst.next())

{
out.println(rst.getString(1));

//out.println("\t\t");

out.println(rst.getString(2));

out.println("<br>");

}catch(Exception e) {

System.out.println(e);

You might also like