Related Topics

JAVA Programing
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>Hello World!</h1>");
out.println("</body></html>");
}
}
In this example, we first import the necessary classes from the javax.servlet.http
package to extend the HttpServlet
class and to handle HTTP requests and responses.
Then, we create a class called HelloWorldServlet
that extends HttpServlet
and overrides the doGet
method. This method is called by the Servlet Container when an HTTP GET request is received for this Servlet.
Inside the doGet
method, we set the content type of the response to “text/html” using the setContentType
method of the HttpServletResponse
object. We then create a PrintWriter
object that we can use to write the response to the client.
Finally, we use the println
method of the PrintWriter
object to output the HTML code that displays the “Hello World!” message in the client’s web browser.
To use this Servlet, we need to deploy it to a Servlet Container such as Apache Tomcat or Jetty. We can do this by packaging the Servlet as a WAR file and deploying it to the Servlet Container using the Container’s administration console or deployment tools. Once deployed, the Servlet should be accessible at a URL that corresponds to the Servlet’s mapping in the web.xml deployment descriptor file.
<form method="POST" action="/my-servlet">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Submit">
</form>
In the Servlet’s doPost method, use the getParameter method of the HttpServletRequest object to retrieve the form data as a String. The getParameter method takes the name of the form field as its argument and returns the value of the field as a String. For example:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// Process the form data here...
}
Note that if the form contains multiple fields with the same name, the getParameter method will return the value of the first field only.
Once you have retrieved the form data, you can process it as needed. For example, you might validate the data, store it in a database, or generate a response based on the data.
Finally, you can use the HttpServletResponse object to generate a response to send back to the client. For example, you might use the setContentType method to set the content type of the response, and then use a PrintWriter to output a response message. For example:
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>Form Submitted Successfully</h1>");
out.println("<p>Username: " + username + "</p>");
out.println("<p>Password: " + password + "</p>");
out.println("</body></html>");
This code sets the content type of the response to “text/html”, creates a PrintWriter
to write the response, and then outputs an HTML response message that includes the form data. The response message can include any other data or HTML code as needed.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.