Servlet中的doGet和doPost

       Servlet就是一个中间的容器来连接Client和Server的。其中比较重要的方法就是doGet和doPost方法了,这两个方法是分别用于获取传递的参数的,千万要注意,获取参数的方法是request的getParameter。就是注意这一点,还有就是设置字体的时候,一定要在getWriter之前,要不然就不能正确的设置字体了。

贴出一个简单的传递参数的例子吧:

这是一个简单的HTML界面:post.html

<!DOCTYPE html>
<html>
  <head>
    <title>post.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
  <form name="myfrom" id="myform" method="post" action="../servlet/methodpost">
  	<table>
  		<tr>
  			<td>
  				name:
  			</td>
  			<td>
  				<input type="text" name="name" id="name" />
  			</td>
  		</tr>
  		<tr>
  			<td>
  				<input type="submit" name="submit" id="submit" value="submit" />
  			</td> 
  		</tr>
  	</table>
  </form>
  </body>
</html>

下面是MethodPost.java

package chapter4;

import java.io.IOException;

public class MethodPost extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public MethodPost() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		String name = request.getParameter("name"); //
		String nameTemp = (String)request.getAttribute("name");
		out.println("name = " + name + "|||| nameTemp = " + nameTemp);
		out.println("<br />");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: doGet和doPostServlet两个常用的方法。 doGet方法用于处理HTTP GET请求,通常用于获取据或页面的显示。在doGet方法,可以通过request对象获取请求参,通过response对象向客户端发送响应据。 doPost方法用于处理HTTP POST请求,通常用于提交据或执行操作。在doPost方法,可以通过request对象获取请求参,通过response对象向客户端发送响应据。 在Servlet,通常会根据具体的业务需求选择使用doGet或doPost方法,或者同时使用两个方法。 ### 回答2: 在JavaServlet是用于处理Web请求和响应的Java类。其的doGet和doPostServlet两个常用的方法。 doGet是一个处理HTTP GET请求的方法。当浏览器发送一个GET请求给Servlet时,Servlet将自动调用doGet方法来处理该请求。通常,doGet方法用于从服务器获取据,如从据库获取信息并返回给浏览器显示。在doGet方法,可以通过HttpServletRequest对象获取请求参,并通过HttpServletResponse对象向浏览器发送响应。 doPost是一个处理HTTP POST请求的方法。当浏览器发送一个POST请求给Servlet时,Servlet将自动调用doPost方法来处理该请求。通常,doPost方法用于向服务器提交据,如通过一个表单向据库插入新的据。在doPost方法,同样可以通过HttpServletRequest对象获取请求参,并通过HttpServletResponse对象向浏览器发送响应。 在Servlet,通常情况下我们会根据具体需求选择使用doGet或doPost方法来处理请求。如果我们只是需要获取一些据,我们可以选择使用doGet方法,因为GET请求在URL会携带请求参,可以直接从URL获取。而如果我们需要向服务器提交据,我们可以选择使用doPost方法,因为POST请求的参是放入请求体,相对安全。另外,对于一些特殊情况,我们也可以重写doGet和doPost方法,使用相同的业务逻辑处理GET和POST请求。 总之,doGet和doPostServlet用于处理GET和POST请求的两个方法,根据具体需求选择使用。 ### 回答3: 在Servlet,doGet()和doPost()都是用于处理HTTP请求的方法。 doGet()方法用于处理GET请求。GET请求是最常见的HTTP请求类型,通常用于获取服务器上的资源。当客户端发送一个GET请求时,Servlet容器会调用doGet()方法来处理该请求。在doGet()方法,可以获取请求参、处理业务逻辑,并且可以通过HttpServletResponse对象将响应据返回给客户端。 doPost()方法用于处理POST请求。POST请求也是常见的HTTP请求类型,与GET请求不同,POST请求通常用于向服务器提交据。当客户端发送一个POST请求时,Servlet容器会调用doPost()方法来处理该请求。在doPost()方法,可以通过HttpServletRequest对象获取请求参、处理业务逻辑,并且可以通过HttpServletResponse对象将响应据返回给客户端。 在Servlet,doGet()和doPost()方法都是被重写的方法,需要在Servlet进行实现。当客户端发送一个HTTP请求时,Servlet容器会根据请求的类型自动调用对应的方法,从而完成请求的处理。 通常情况下,GET方法用于获取资源,而POST方法用于提交据。在实际应用,可以根据具体的需求选择使用doGet()或者doPost()方法来处理不同类型的请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值