0% found this document useful (0 votes)
34 views2 pages

Jakarta Servlet HTML Response Example

The document contains two Java servlet classes: Welcome and DateTimeServlet. The Welcome class generates a simple HTML response greeting users, while the DateTimeServlet class responds with the current date and time in an HTML format. Both classes override the doGet method to handle HTTP GET requests and set the response content type to text/html.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

Jakarta Servlet HTML Response Example

The document contains two Java servlet classes: Welcome and DateTimeServlet. The Welcome class generates a simple HTML response greeting users, while the DateTimeServlet class responds with the current date and time in an HTML format. Both classes override the doGet method to handle HTTP GET requests and set the response content type to text/html.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

*;
import [Link].*;
import [Link].*;

public class Welcome extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
// Set content type for the response
[Link]("text/html");

// Get the writer to write the response


PrintWriter out = [Link]();

// Generate a simple HTML response


[Link]("<html><body>");
[Link]("<h1>Hello, Jakarta Servlet!</h1>");
[Link]("</body></html>");
}
}

import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;

public class DateTimeServlet extends HttpServlet {


// Override the doGet method to handle GET requests
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {

// Set the response content type


[Link]("text/html");

// Get the PrintWriter object to write response data


PrintWriter out = [Link]();

// Get the current date and time


Date currentDate = new Date();

// Generate HTML response with current date and time


[Link]("<html>");
[Link]("<head><title>Current Date and
Time</title></head>");
[Link]("<body>");
[Link]("<h2>Current Date and Time of
Server:</h2>");
[Link]("<p>" + [Link]() + "</p>");
[Link]("</body>");
[Link]("</html>");

// Close the PrintWriter


[Link]();
}
}

You might also like