GET和POST方式提交请求的不同

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>read the information of cookie</title>
</head>
<body>
<!-- 使用get方式向request.jsp传递参数,后者会只接收此链接传递的参数 -->
<a href="request.jsp?id=5&name=tom">request用法</a>
<p>
<!-- 使用post方式向request.jsp传递参数,后者只接收post来的参数 -->
<form action="request.jsp" method="post">
<table>
<tr>
<td>id of the patient:<input type="text" name="id" value="nameofpatient"> </td>
<td>name of the patient:<input type="text" name="name" value="nameofpatient"> </td>
</tr>
<tr>
<td>submit: <input type="submit" value=submit> </td>
</tr></p>
</table>
</form>
<!-- 应用cookie -->



</body>
</html>

可以看到get方式提交的request带的参数是5和tom,而post方式带的参数是
nameofpatient和idofpatient。request.jsp以get或post方式接收参数的代码均是

<%
String idString=request.getParameter("id");
String nameString=request.getParameter("name");
%>

注意<% %>,提示此代码为java代码片断。
取得相关参数后,以request.setAttribute方式提交将相关参数附加到request上,然后
通过<jsp:forward page=“result.jsp”/>转发request到result.jsp。此时携带有相关参数。
在result.jsp中通过getAttribute取出参数。
<%String idresult=request.getAttribute("id_result").toString();%> the id of the patient :<%=idresult %> <br> <%String nameresult=request.getAttribute("name_result").toString();%> the name of the patient:<%=nameresult %>

setAttribute中前面参数为String name,后面参数为Object object。getAttribute取出的参数为object型,需通过.toString()转换为String类型。

发布了28 篇原创文章 · 获赞 0 · 访问量 348

猜你喜欢

转载自blog.csdn.net/weixin_45003282/article/details/102584775