request应用
java类
package com. yan. Request ;
import javax. servlet. ServletException ;
import javax. servlet. http. HttpServlet ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. util. Arrays ;
public class loginServelet extends HttpServlet {
@Override
protected void doGet ( HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException {
String username = req. getParameter ( "username" ) ;
String password = req. getParameter ( "password" ) ;
String [ ] hobbys = req. getParameterValues ( "hobbys" ) ;
System . out. println ( username) ;
System . out. println ( password) ;
System . out. println ( Arrays . toString ( hobbys) ) ;
req. getRequestDispatcher ( req. getContextPath ( ) + "/success.jsp" ) . forward ( req, resp) ;
}
@Override
protected void doPost ( HttpServletRequest req, HttpServletResponse resp) throws ServletException , IOException {
doGet ( req, resp) ;
}
}
web.xml
< servlet>
< servlet- name> doGet< / servlet- name>
< servlet- class > com. yan. Request. loginServelet< / servlet- class >
< / servlet>
< servlet- mapping>
< servlet- name> doGet< / servlet- name>
< url- pattern> / doGet< / url- pattern>
< / servlet- mapping>
index.jsp
< % @ page contentType= "text/html;charset=UTF-8" language= "java" % >
< html>
< title> 登录< / title>
< body>
< h2> Hello World ! < / h2>
< div style= "text-align: center" >
< form action= "${pageContext.request.contextPath}/login" method= "post" >
< % -- 以post方式提交表单,提交到我们的login请求-- % >
用户名: < input type= "text" name= "username" > < br>
密码:< input type= "password" name= "password" > < br>
爱好:
< input type= "checkbox" name= "girl" value= "女孩" > 女孩
< input type= "checkbox" name= "code" value= "代码" > 代码
< input type= "checkbox" name= "sing" value= "唱歌" > 唱歌
< input type= "checkbox" name= "电影" value= "电影" > 电影
< br>
< input type= "submit" >
< / form>
< / div>
< / body>
< / html>
success.jsp
< % --
Created by IntelliJ IDEA.
User : chaojixingyun
Date : 2022 / 8 / 19
Time : 15 : 15
To change this template use File | Settings | File Templates .
-- % >
< % @ page contentType= "text/html;charset=UTF-8" language= "java" % >
< html>
< head>
< title> success< / title>
< / head>
< body>
< h2> 登录成功< / h2>
< / body>
< / html>
response
String username = req. getParameter ( "username" ) ;
String pwd = req. getParameter ( "pwd" ) ;
System . out. println ( username + ":" + pwd) ;
resp. sendRedirect ( "/response_war_exploded/success.jsp" ) ;
resp. setContentType ( "text/http" ) ;
resp. setCharacterEncoding ( "utf-8" ) ;
String realPath= "F:\\IDEAstudy\\ServeletStudy\\response\\src\\main\\resources\\1.png" ;
System . out. println ( "获取到的路径:" + realPath) ;
String filename = realPath. substring ( realPath. lastIndexOf ( "//" ) + 1 ) ;
resp. setHeader ( "Content-disposition" , "attachment;filename=" + URLEncoder . encode ( filename, "utf-8" ) ) ;
FileInputStream in = new FileInputStream ( realPath) ;
int len= 0 ;
byte [ ] buffer = new byte [ 1024 ] ;
ServletOutputStream out = resp. getOutputStream ( ) ;
while ( ( len= in. read ( buffer) ) != - 1 ) {
out. write ( buffer, 0 , len) ;
}
in. close ( ) ;
out. close ( ) ;