0% found this document useful (0 votes)
7 views15 pages

Practical 1 AJAVA1

Uploaded by

ay9219532407
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)
7 views15 pages

Practical 1 AJAVA1

Uploaded by

ay9219532407
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/ 15

Name: Satyajit Gorad

Roll No:5287
Class:SYCS-A

Practical 1

AIM : Creating Simple web pages using Servlet


Q1] Create a simple servlet that prints "Hello, World!" on the web page.

Pseudo Code
Step 1: Create a new Java Web project in NetBeans with the name "Hello".
Step 2: Create index.html in the Web Pages folder and specify a form with action as "satyajit".
Step 3: Add a single input button in index.html with the value "Generate HTML".
Step 4: Create a new Servlet named "satyajit5287" in the Source Packages folder.
Step 5: Implement the doGet() method in the satyajit5287 servlet to generate an HTML response that
includes:
• A heading "Hello, World!".
• A subheading "5287 Satyajit".
Step 6: Run the project and open index.html.
Step 7: Click the "Generate HTML" button and observe the servlet-generated response showing both
headings.

Index.html
<html>
<head>

<meta charset="UTF-8">
<title>Generate HTML </title>
</head>
<body>
<form action="satyajit5287" method="get">
<input type="submit" value="Generate HTML">
</form>
</body>
</html>

satyajit5287.java

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

public class satyajit5287 extends HttpServlet {


Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html");

PrintWriter out = response.getWriter(); out.println("<html><head><title>Hello


Satyajit!</title></head><body>");out.println("<h1>Hello satyajit!<h1>");
out.println("<head>");
out.println("<title>Servlet satyajit5287</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>satyajit5287 " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
Output:

}
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

2] Create a simple servlet that shows the time-table of your class on the web page.

Pseudo Code

Step 1: Create a new Java Web project in NetBeans with the name "satyajit5287".
Step 2: Add an index.html file in the Web Pages folder.
Step 3: In index.html, create a form with the action "TableServlet" and method "GET".
Step 4: Add a button in the form with the label "Generate HTML".
Step 5: Create a new servlet named prac1secondjava in the Source Packages folder.
Step 6: Use the @WebServlet("/table") annotation for mapping the servlet.
Step 7: Implement the doGet() method in the servlet to generate an HTML response with a table.
Step 8: In the HTML table, create three columns: Time, Teachers, and Subject.
Step 9: Add three rows in the table with sample timetable data.
Step 10: Use the PrintWriter object to dynamically print the HTML table. Step 11: Run the project and
open index.html.
Step 12: Click the "Generate HTML" button to view the timetable.

Code:
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Generate HTML</title>
</head>
<body>
<form action="TableServlet" method="post">
<input type="submit" value="Generate HTML">
</form>
</body>
</html>

TableServlet.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;
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
@WebServlet("/table")
public class TableServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html><head><title>Table Example</title></head><body>");
out.println("<h2>Simple Table</h2>");
out.println("<table border=\"1\">");
out.println("<tr><th>Time</th><th>Teacher</th><th>Subject</th></tr>");
out.println("<tr><td>7:45</td><td>Teacher1</td><td>Sub1</td></tr>");
out.println("<tr><td>8:30</td><td>Teacher2</td><td>Sub2</td></tr>");
out.println("<tr><td>9:15</td><td>Teacher3</td><td>Sub3</td></tr>");
out.println("</table>");
out.println("</body></html>");
}
}

Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

3] Create a servlet that generates an HTML response containing a simple webpage structure
with a heading and a paragraph and Imgage.

Pseudo code:

Step 1: Create a new Java Web project in NetBeans with the name "5287satyajiygo".
Step 2: Add an index.html file in the Web Pages folder.
Step 3: In index.html, add a heading with "5287 Satyajit" and "Welcome to Index Page".
Step 4: Add a paragraph with a link: "Click here" that points to the servlet " SimplePageServlet ".
Step 5: Create a new servlet named SimplePageServlet in the Source Packages folder.
Step 6: Use the @WebServlet("/simplePage") annotation to map the servlet.
Step 7: Implement the doGet() method in the servlet to generate a simple HTML page with a
heading and a paragraph.
Step 8: Add an image in the servlet response with the source URL .
Step 9: Set the response content type to "text/html".
Step 10: Run the project and open index.html.
Step 11: Click the link in index.html to view the page generated by the servlet.

Code:
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Index Page</title>
</head>
<body>
<h1>Welcome to SATYAJIT!! </h1>
<p>This is the index page.</p>
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
<p>Click <a href="SimplePageServlet">here</a> to view the Simple Web Page generated by the
servlet.</p>
</body>
</html>

SimpleServlet.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;

@WebServlet("/simplePage")
public class SimplePageServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html><head><title>Simple Web Page</title></head><body>");


out.println("<h1>Welcome to My Web Page</h1>");
out.println("<p>This is a simple web page generated by a servlet.</p>");
out.println("<img
src="https://i.pinimg.com/236x/72/a2/8d/72a28dad15ce74ff691914cc3ecaad7e.jpg" alt=\"Peacock
Image\">");
out.println("</body></html>");
}
}
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
Output:

.
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A
4] Create a servlet in Java that generates an HTML page with a basic homepage layout
consisting of a header, navigation menu, content area, and footer.

Step 1: Create a new project in NetBeans named "5287satyajitgo".


Step 2: Create an index.html file in the Web Pages folder of your project.
Step 3: In index.html, add a heading: "Welcome to Index Page" and a link to "HomePageServlet"`
servlet to navigate to the HOME Page.
Step 4: Create a new servlet named prac1fourthmain.java inside the HomePageServlet package.
Step 5: Map the servlet to the URL pattern /homepage using @WebServlet("/homepage").
Step 6: Set content type to "text/html" in the doGet() method.
Step 7: Generate an HTML page with a custom header, navigation bar, content section, and footer.
Step 8: In the servlet, use inline CSS to style the page with a background color, text color, and
layout (header, navigation, content, and footer).
Step 9: Add navigation links (Home, About, Contact) in the nav section.
Step 10: Display content in the content section with your personal details: Name, Roll No, and
Class.
Step 11: Add a footer to the page with a copyright message "© 2024 My Website. All rights
reserved.".
Step 13: Run the project, open index.html, and click on the link to navigate to the Home Page
generated by the servlet
Code:
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Index Page</title>
</head>
<body>
<h1>Welcome Satyajit to Index Page</h1>

<p>This is the index page.</p>


<p>Click <a href="HomePageServlet">here</a> to view the HOME Page</p>
</body>
</html>
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

HomePageServlet.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;

@WebServlet("/homepage")
public class HomePageServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>My Home Page</title>");
out.println("<style>");
out.println("body {font-family: Arial, sans-serif;}");
out.println("header {background-color: #333; color: white; padding: 20px; text-align: center;}");
out.println("nav {float: left; width: 20%; background: #f2f2f2; padding: 20px;}");
out.println("nav ul {list-style-type: none; padding: 0;}");
out.println("nav ul a {text-decoration: none;}");
out.println(".content {float: left; width: 80%; padding: 20px;}");
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

out.println("footer {background-color: #333; color: white; text-align: center; padding: 10px;}");


out.println("</style>");
out.println("</head>");
out.println("<body>");

// Header
out.println("<header>");
out.println("<h1>Welcome Satyajit to My Website</h1>");
out.println("</header>");

// Navigation
out.println("<nav>");
out.println("<ul>");
out.println("<li><a href=\"#\">Home</a></li>");
out.println("<li><a href=\"#\">About</a></li>");
out.println("<li><a href=\"#\">Contact</a></li>");
out.println("</ul>");
out.println("</nav>");

// Content
out.println("<div class=\"content\">");
out.println("<h2>Home Page Content Goes Here</h2>");
out.println("<p>This is a basic homepage layout generated by a servlet.</p>");
out.println("</div>");

// Footer
out.println("<footer>");
out.println("<p>© 2024 My Website. All rights reserved.</p>");
out.println("</footer>");

out.println("</body>");
out.println("</html>");
}
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

5] Create a Java servlet to showcase the syllabus for Second Year Computer Science
(SYCS) on a web page.

Pseudo Code:
Step 1: Create index.html page – Add a welcome message and a link to the
SYCSSyllabusServlet servlet for viewing the syllabus.
Step 2: Create SYCSSyllabusServlet.java servlet – Write a servlet that generates an
HTML page displaying the SYCS syllabus.
Step 3: Map servlet with @WebServlet("/sycs-syllabus") – Ensure the servlet is
accessible via the /sycs-syllabus URL.
Step 4: Display syllabus content – Inside the servlet, generate HTML content listing
subjects for both first and second semesters.
Step 5: Include author information – Add the name " Rohit Tripathi 5364 SYCS Syllabus
" in the header of the syllabus page.

Code:
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Index Page</title>
</head>
<body>
<h1>Welcome to Index Page</h1>
<p>This is the index page.</p>
<p>Click <a href="SYCSSyllabusServlet">here</a> to view the Syllabus</p>
</body>
</html>
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

SYCSSyllabusServlet.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;

@WebServlet("/sycs-syllabus")
public class SYCSSyllabusServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

// HTML content for the syllabus


String syllabusContent = "<h2>SYCS Syllabus</h2>" +
"<h3>First Semester</h3>" +
"<ul>" +
"<li>Subject 1: Introduction to Programming</li>" +
"<li>Subject 2: Data Structures and Algorithms</li>" +
"<li>Subject 3: Database Management Systems</li>" +
"<li>Subject 4: Computer Organization and Architecture</li>" +
"<li>Subject 5: Discrete Mathematics</li>" +
"</ul>" +
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

"<h3>Second Semester</h3>" +
"<ul>" +
"<li>Subject 1: Object-Oriented Programming with Java</li>" +
"<li>Subject 2: Software Engineering Principles</li>" +
"<li>Subject 3: Operating Systems</li>" +
"<li>Subject 4: Web Programming</li>" +
"<li>Subject 5: Communication Skills</li>" +
"</ul>";

// Generate HTML response


out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>SYCS Syllabus</title>");
out.println("</head>");
out.println("<body>");
out.println(syllabusContent);
out.println("</body>");
out.println("</html>");
}
}

Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A

You might also like