Java Servlets   «Prev  Next»

First Java Servlet - Exercise Result

Your first real servlet


You Said: When you load the servlet URL into your browser, you should see this:
What Helloworld servlet should look like in a browser
What Helloworld servlet should look like in a browser

The following example contains the HTML code for a form that posts first_name and last_name to a Java Servlet.
<!DOCTYPE html>
<html>
<head>
    <title>Submit Form to Java Servlet</title>
</head>
<body>
    <form action="YourServletURL" method="POST">
        <label for="first_name">First Name:</label> 
        <input type="text" id="first_name" name="first_name"> 
        <label for="last_name">Last Name:</label> 
        <input type="text" id="last_name" name="last_name">
        <input type="submit" value="Submit">
    </form>
</body>
</html>

This HTML form, when submitted, will send a POST request to the URL specified in the action attribute of the form element. The method attribute of the form element is set to POST indicating the HTTP method to use when sending form data.
The input elements with id and name attributes of first_name and last_name are the fields that users will fill out. The name attributes of these input elements (i.e., first_name and last_name) are the keys that will be sent in the HTTP POST request to the Java Servlet.
The servlet would then access these values with the request.getParameter("first_name") and request.getParameter("last_name") methods respectively, where request is an instance of HttpServletRequest.
Ensure to replace "YourServletURL" with the actual URL of your Java Servlet.
Please note that in a real-world scenario, form data should be validated and sanitized to prevent security issues such as Cross-Site Scripting (XSS) and SQL Injection. Also, using HTTPS (SSL/TLS) for form submission is critical to prevent data from being intercepted during transmission.

The following is the code for a Java Servlet that accepts 2 parameters from a HTML Form for the 2 parameters
1) first_name and 2) last_name.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class NameServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // Set response content type
        response.setContentType("text/html");

        // Obtain parameters from POST request
        String firstName = request.getParameter("first_name");
        String lastName = request.getParameter("last_name");

        // Validation would typically be performed here

        PrintWriter out = response.getWriter();

        String title = "Data Received";
        String docType = "\n";

        out.println(docType +
		        "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<ul>\n" +
                "  <li><b>First Name</b>: " + firstName + "\n" +
                "  <li><b>Last Name</b>: " + lastName + "\n" +
                "</ul>\n" +
                "</body></html>");
    }
}

Servlet Code Explanation

This servlet extends the HttpServlet class and overrides the doPost() method to handle POST requests.
The HttpServletRequest.getParameter() method is used to retrieve the first_name and last_name parameters from the request.
The response.setContentType() method sets the response's content type to text/html. The servlet writes the HTML response to the client using the PrintWriter obtained from HttpServletResponse.getWriter(). The response includes the first_name and last_name parameters received from the form.
For the security of the application, any form data should be validated and sanitized before processing. This basic servlet does not include any form of input validation or sanitization, which would be necessary in a production application.