0% found this document useful (0 votes)
3 views

task 8

The document outlines the steps to install and configure the Eclipse IDE for web application development, including connecting a Tomcat server for servlet programs. It provides code examples for two servlets, HelloServlet and ConnectServlet, demonstrating basic servlet functionality and database connectivity using MySQL. The document serves as a practical guide for setting up a development environment and creating simple web applications.
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)
3 views

task 8

The document outlines the steps to install and configure the Eclipse IDE for web application development, including connecting a Tomcat server for servlet programs. It provides code examples for two servlets, HelloServlet and ConnectServlet, demonstrating basic servlet functionality and database connectivity using MySQL. The document serves as a practical guide for setting up a development environment and creating simple web applications.
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/ 8

Web Application Development and

Software Frameworks Lab (CSEN2091P)

Experiment-8
M.VANSHIKA

VU21CSEN03000016

Steps to install and configure Eclipse IDE:

● Go to
https://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/rele
a se/2022-09/R/eclipse-java-2022-09-R-win32-x86_64.zip and download a suitable
installer into your computer

● Once the installer is downloaded, click on it to open ● Click on launch to open


Eclipse IDE

● The configured and installed IDE will look as below.


● The next step is to connect our Tomcat server within the IDE to use it for our servlet
programs.
Web Application Development and
Software Frameworks Lab (CSEN2091P)

Steps to configure Tomcat:


● Click on help and select install new software
● Select work with as shown below and select the web,XML to install it
Web Application Development and
Software Frameworks Lab (CSEN2091P)

● On right content you will find open perspective.In that select Java EE and
install it.Wait until the installation completes and restart it. ● Create
files as shown below

HelloServlet.java
import java.io.IOException; import
java.io.PrintWriter; import
javax.servlet.ServletException; import
javax.servlet.annotation.WebServlet; import
javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class HelloServlet
*/
@WebServlet("/HelloServlet") public class
HelloServlet extends HttpServlet { private static final
long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
Web Application Development and
Software Frameworks Lab (CSEN2091P)

public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub

response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.println("<h1>Hello</h1>");

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

Right click on java file and run with server

Output:
Web Application Development and
Software Frameworks Lab (CSEN2091P)

DBServlet.java:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException; import


javax.servlet.annotation.WebServlet; import
javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class ConnectServlet
*/
@WebServlet("/ConnectServlet") public class
ConnectServlet extends HttpServlet { private static
final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public ConnectServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)
*/
Web Application Development and
Software Frameworks Lab (CSEN2091P)

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
// TODO Auto-generated method stub response.setContentType("text/html");
PrintWriter pw=response.getWriter();
//initializing connections
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");

con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root", “password”l);
stmt=con.createStatement();
rs=stmt.executeQuery("Select * from User");
while(rs.next()) {

pw.print("<br>"+rs.getInt(1)+","+rs.getString("Name")+","+rs.getString("Email"));
}
}
catch(Exception e) {
e.printStackTrace();
}
; }

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}}

MySQL:
Web Application Development and
Software Frameworks Lab (CSEN2091P)
Web Application Development and

You might also like