3.doGet,doPost和service的用法
1)doPost的实例
马克-to-win:我们看到了doGet的用法。这节,我们看看doPost的用法。在Eclipse下,点击你项目名称左边的小十字符号,找到WebContent,右击它。New/HTML File:
eclipse会为我们生成一个空的html。我们再加入一些代码,成为:
例:3.3.1
hello.html:
<html>
<body>
<FORM ACTION="MarkToWinServletHello1"
METHOD="POST">
First Name:
<INPUT TYPE="TEXT" NAME="firstName"><BR>
Shipping Address:
<TEXTAREA NAME="address" ROWS=3 COLS=40></TEXTAREA><BR>
Credit Card:<BR>
<INPUT TYPE="RADIO" NAME="cardType"
VALUE="Visa">Visa<BR>
<INPUT TYPE="RADIO" NAME="cardType"
VALUE="Master Card">Master Card<BR>
<INPUT TYPE="SUBMIT" VALUE="Submit Order">
</CENTER>
</FORM>
</body>
</html>
更改一下我们的ServletHello1.java,成为:
package com;
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 ServletHello1 extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String fn=request.getParameter("firstName");
String ad=request.getParameter("address");
String ct=request.getParameter("cardType");
PrintWriter pw=response.getWriter();
pw.println(fn " " ad " " ct);
}
}
更多请看:https://blog.csdn.net/qq_44594371/article/details/103155497