Practical 1 AJAVA1
Practical 1 AJAVA1
Roll No:5287
Class:SYCS-A
Practical 1
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;
}
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 {
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 {
.
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.
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 {
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
// 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();
"<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>";
Output:
Name: Satyajit Gorad
Roll No:5287
Class:SYCS-A