Servlet

Servlet Read Form Data

This Servlet tutorial is to take you to the next step in learning servlets. 

As stated earlier in start of the servlet tutorial series, servlets are primarily meant for web applications. 

In this let us see how we can read html form data from a URL and process it in a servlet and then send the response back to the client.

GET and POST method

As seen earlier in Servlet terminologies, GET and POST methods are used to pass information from a form to a Java Servlet

While using GET method the form data is passed in the url as query parameters. GET method is the default method used. It looks like,

http://localhost:8080/hello?key1=value1&key2=value2

If POST method is used then the form data is passed in the HTTP request message body. 

It cannot be seen in the URL as seen in GET method. URL length has a limitation and so if we are passing large volume of data we should use POST. Similarly sensitive data like passwords should also be passed using POST method.

Read Form Data

we have methods doXYZ(…) in the HTTPServlet class. 

The method doGet(…) is invoked by the servlet container if the form uses GET method or doPost(…) if the the form uses POST method. 

In this example we are using GET method.

package com.javapapers.servlet.introduction;

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

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");

		PrintWriter out = response.getWriter();
		String title = "Servlet: Read Form Data";
		out.println("<html>" + "<head><title>" + title
				+ "</title></head><body>"
				+ "<h1>" + title + "</h1>"
				+ "<p>Hi "
				+ request.getParameter("name")
				+ "</p>"
				+ "</body></html>");
	}
}

web.xml

This web.xml defines the Servlet mapping for the form data servlet.

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <display-name>Servlet Form Data Handling</display-name>
    <servlet>
        <servlet-name>HelloFormData</servlet-name>
        <servlet-class>com.javapapers.servlet.introduction.HelloFormData</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloFormData</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Servlet Read Form Data</title>
</head>
<body>
<form action="./hello" method="GET">
Enter your Name: <input type="text" name="name">
<input type="submit" value="Submit" />
</form>
</body>
</html>

Read using POST method

In the above HTML page in form tag instead of GET use as POST. 

Then in the servlet to read the form data add a doPost method as below,

 public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
     doGet(request, response);
  }

Servlet-Read-Form-Data

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值