doGet và doPost trong Servlet

Về doGet và doPost trong Servlet có thể hiểu cơ bản như sau:

  • doGet để xử lý các request có phương thức là get ( nó chính là các url chứa tham số )
  • doPost để xử lý các request có phương thức là post ( nó chính là request đến từ form)

1. doGet trong Servlet

Tạo file SampleGetData.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Sample Get Data</h1>
<form action="SampleGetDataServlet" method="get">
Nhap Ten : <input type="text" name="name" /> <input type="submit"
value="Send" />
</form>

</body>
</html>

Tạo class SampleGetDataServlet.java

package com.loop.servlet.io;

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 SampleGetDataServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  /**
  * Create by Cuder
  *
  */
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    PrintWriter out;
    String name = req.getParameter("name");
    res.setContentType("text/html; charset=UTF-8");
    out = res.getWriter();
    out.println("<html><body>");
    out.println("<h1>Ten vua nhap : " + name + "</h1>");
    out.println("</body></html>");
  }
}

Thêm nội dung mapping sau vào file web.xml

<servlet>
<servlet-name>SampleGetDataServlet</servlet-name>
<servlet-class>com.loop.servlet.io.SampleGetDataServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>SampleGetDataServlet</servlet-name>
<url-pattern>/SampleGetDataServlet/*</url-pattern>
 </servlet-mapping>

Kết quả sau khi run

doGet và doPost trong Servlet
doGet và doPost trong Servlet

2. doPost trong Servlet

Tạo file SamplePostData.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Sample Post Data</h1>

<form action="SamplePostDataServlet" method="post">
Nhap Ten : <input type="text" name="name" /> <input type="submit"
value="Send" />
</form>

</body>
 </html>

Tạo class SamplePostDataServlet.java

package com.loop.servlet.io;

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 SamplePostDataServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  /**
  * Create by Cuder
  *
  */
  public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    PrintWriter out;
    String name = req.getParameter("name");
    res.setContentType("text/html; charset=UTF-8");
    out = res.getWriter();
    out.println("<html><body>");
    out.println("<h1>Ten vua nhap : " + name + "</h1>");
    out.println("</body></html>");
  }
}

Thêm nội dung mapping sau vào file web.xml

<servlet>
<servlet-name>SamplePostDataServlet</servlet-name>
<servlet-class>com.loop.servlet.io.SamplePostDataServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>SamplePostDataServlet</servlet-name>
<url-pattern>/SamplePostDataServlet/*</url-pattern>
 </servlet-mapping>

Kết quả sau khi run

doGet và doPost trong Servlet
doGet và doPost trong Servlet
Rate this post
Click Here to Leave a Comment Below 0 comments