获取POST请求中的参数方法
1.application/x-www-form-urlencoded:类似query-String的格式
import javax. servlet. annotation. WebServlet ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
@WebServlet ( "/postParameter" )
public class PostParameter extends HttpServlet {
@Override
protected void doPost ( HttpServletRequest req, HttpServletResponse res) throws IOException {
res. setContentType ( "text/html;charset=utf-8" ) ;
String aa= req. getParameter ( "aa" ) ;
String bb= req. getParameter ( "bb" ) ;
res. getWriter ( ) . write ( String . format ( "aa: %s&bb: %s <br>" ,
aa, bb) ) ;
}
}
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
< html>
< head>
< title> Title</ title>
</ head>
< meta http-equiv = " Content-Type" content = " text/html;charset=utf-8" />
< body>
< form action = " postParameter" method = " POST" >
< input type = " text" name = " aa" >
< input type = " text" name = " bb" >
< input type = " submit" value = " 提交" >
</ form>
</ body>
</ html>
2.mutlipart/form-data
3.application/json
import javax. servlet. annotation. WebServlet ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. io. InputStream ;
@WebServlet ( "/postParameterJson" )
public class PostParameterJsonServlet extends HttpServlet {
@Override
protected void doPost ( HttpServletRequest req, HttpServletResponse resp) throws IOException {
String body= readBody ( req) ;
resp. getWriter ( ) . write ( body) ;
}
private String readBody ( HttpServletRequest req) throws IOException {
InputStream inputStream= req. getInputStream ( ) ;
int length= req. getContentLength ( ) ;
byte [ ] bytes= new byte [ length] ;
inputStream. read ( bytes) ;
return new String ( bytes, "utf-8" ) ;
}
}
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
< html>
< head>
< title> Title</ title>
</ head>
< meta http-equiv = " Content-Type" content = " text/html;charset=utf-8" />
< body>
< button onclick = " sendJson()" > 发送请求</ button>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js" > </ script>
< script>
function sendJson ( ) {
let body= {
aa: 10 ,
bb: 20
} ;
$. ajax ( {
url: "postParameterJson" ,
type: "post" ,
contentType: "application/json;charset=uft-8" ,
data: JSON . stringify ( body) ,
success : function ( body, status ) {
console. log ( body) ;
}
} )
}
</ script>
</ body>
</ html>
4.引入JSON库,解析json
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
< html>
< head>
< title> Title</ title>
</ head>
< meta http-equiv = " Content-Type" content = " text/html;charset=utf-8" />
< body>
< button onclick = " sendJson()" > 发送请求</ button>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js" > </ script>
< script>
function sendJson ( ) {
let body= {
aa: 10 ,
bb: 20
} ;
$. ajax ( {
url: "Json2" ,
type: "post" ,
contentType: "application/json;charset=uft-8" ,
data: JSON . stringify ( body) ,
success : function ( body, status ) {
console. log ( body) ;
}
} )
}
</ script>
</ body>
</ html>
import com. fasterxml. jackson. databind. JsonMappingException ;
import com. fasterxml. jackson. databind. ObjectMapper ;
import javax. servlet. ServletException ;
import javax. servlet. annotation. WebServlet ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. io. InputStream ;
class JsonData {
public int aa;
public int bb;
}
@WebServlet ( "/Json2" )
public class JSON2 extends HttpServlet {
@Override
protected void doPost ( HttpServletRequest req, HttpServletResponse resp) throws IOException {
String body= readBody ( req) ;
ObjectMapper objectMapper= new ObjectMapper ( ) ;
JsonData jsonData= null ;
try {
jsonData = objectMapper. readValue ( body, JsonData . class ) ;
} catch ( JsonMappingException e) {
e. printStackTrace ( ) ;
}
resp. getWriter ( ) . write ( String . format ( "aa:%d;bb:%d<br>" , jsonData. aa, jsonData. bb) ) ;
}
public static String readBody ( HttpServletRequest req) throws IOException {
InputStream inputStream= req. getInputStream ( ) ;
int length= req. getContentLength ( ) ;
byte [ ] butter= new byte [ length] ;
inputStream. read ( butter) ;
return new String ( butter, "utf-8" ) ;
}
}