文章目录 1.nginx的conf文件:以前网络编程中b/s架构中服务器是用socket写,用文件输入流读一个文件,读到后socket通过outputstream写出去,这些过程有了nginx后再也不用写 2.nginx反向代理与负载均衡:nginx是被广泛使用的网页服务器,在www.163.com的f12的network中headers中有server:nginx,使用nginx首当其冲,做反向代理,将请求分发到公司的多台服务器上 2.1 location匹配方式:location / 默认匹配所有请求,相当于第四优先级(最弱) 2.2 反向代理写法:http80, 2.3 负载均衡写法:基于反向代理 3.服务器:软件为tomcat或nginx,程序为servlet 4.servlet:多态,web.xml 5.servlet生命周期:lifecycleservlet 6.servlet优化:genericservelt/httpservelt 7.http协议和抓包:枚举enumeration,request.getheadernames() 8.request对象:.getparametermap(),post请求中文参数乱码 9.请求转发:jrss,request.setattribute() 10.登陆案例(1):成功和失败页面相当于两个模块 11.response对象:text/html,重定向(.setstatus,.setheader) 12.文件下载:request/response
nginx软件链接:https://pan.baidu.com/s/1zvf2iri6oendkvfvsiozmg ,提取码:gsn9。 如果http协议是80端口的话,80端口会被隐藏,本机浏览器输入http://localhost:80启动服务端显示如下,hello nginx是e\my81\index.html里内容。本机ipconfig显示192.168.33.71。上面红框浏览器(客户端)是别人的电脑【局域网下同一网段】,上面两个大框是服务端,浏览器和服务器三要素对应。 nginx作用:1.反向代理:s被代理1个公网ip,但有n台s对外服务。 2.负载均衡:基于反向代理。 3.正向代理:我们电脑接入vpn 后,我们对外ip地址就会变成vpn服务器的公网ip。 4.动静分离:网页f12显示js,css,img属静态数据。
线程通讯:volatile关键字,wait(),notify() 线程安全:synchronized关键字 计算密集型:进行(多核) io密集型:线/协程(只1个核在做,占资源轻,io等待时间拿来用)
原生nginx没有集成很多插件,使用不方便。推荐使用openresty(在nginx基础上集成了很多lua写的插件),凯发k8官方网官网下载openresty后解压后进一级目录输入nginx.exe回车运行服务端,浏览器输入localhost默认:80端口。修改conf目录下nginx.conf文件,删除注释。50x.html是错误页面,暂时不用。 如下还是在nginx.conf文件里,stream类型执行下载,echo是插件。
2.1 location匹配方式:location / 默认匹配所有请求,相当于第四优先级(最弱)
1.如下最强级别=。 2.如下优先级第二,^~以什么开头。如下/是路径,不是转义符。 3.如下优先级第三,正则表达式~。\w匹配数字、字母、下划线,转义字符\可以转义很多字符,比如\n表示换行,\t表示制表符,字符\本身也要转义,所以\\表示的字符就是\。如下/是路径。 如下同优先级的,按匹配程度较高的先匹配。 同优先级并且匹配程度相同的话,写在上面的优先执行。
2.2 反向代理写法:http80,
如下是其他服务器,记住192…:80是it works页面。 如下localhost先到本机127.0.0.0,再转到192上面的这个服务器。 如下是apach返回的not found返回的信息,192…:80/a路径没有,不是nginx返回的信息,因为已经把请求转到192… 1.默认拼接。 如下其实转到了192...:80/a。 2.如下转到192...:80/,两个/省去后一个字母。
2.3 负载均衡写法:基于反向代理
两个ip 端口根据权重切换。
web(world wide web)即全球广域网,也称为万维网。 nginx不支持java规范,tomcat支持。tomcat8:https://tomcat.apache.org/download-80.cgi。免安装,解压即可用:链接:https://pan.baidu.com/s/1ujm9kbihgixknxlylpcogw ,提取码:g610。dos系统识别文件后缀名不能超过3位(先有dos后有windows),所以.htm。8080一般是用来连接代理的(8080不能省,只有80才能省)。 只有index.html是默认的,不用加在最后,localhost:8080/a.html。 如下javaee新建项目,apache-tomcat8…是前面tomcat软件解压路径。如下不需要自己开启tomcat,idea配置自动开启。 如下new和上面一样,关联tomcat软件解压路径。 如下将web文件夹当成前端static web项目,web文件夹里都可以访问,除了web-info无法访问(如【mysql3】最后一节,-info文件夹放jar包),toolbar工具栏。 如下是点乌龟运行后,浏览器自动打开的原理。改变路径 / 写法。 如上若还是不会默认打开谷歌,需要file - settings - tools - web browsers。
如下选中src右击new - java class,name为com.itheima01.servlet.myservlet,还有一件事配置web.xml如上图所示。
package com. itheima01. servlet ;
import javax. servlet. * ;
import java. io. ioexception ; public class myservlet implements servlet { @override public void service ( servletrequest servletrequest
, servletresponse servletresponse
) throws servletexception , ioexception { system . out
. println ( "控制台日志: 服务器被访问了" ) ; servletresponse
. getwriter ( ) . print ( "hello servlet" ) ; } @override public void init ( servletconfig servletconfig
) throws servletexception { } @override public servletconfig getservletconfig ( ) { return null ; } @override public string getservletinfo ( ) { return null ; } @override public void destroy ( ) { }
}
如下点击idea中乌龟,自动打开网页,改变/my路径。 tomcat servlet原理(4步):以前解析xml文件(web.xml)用dom4j,将xml先读到内存里在进行解析。第4步:mapping映射后,tomcat(中介即代理默认端口8080)底层会进行反射,当前类com.itheima01.servlet.myservlet肯定是servlet接口的implements实现类,可以向上转型,父类servlet调用方法执行子类重新的方法。
为什么tomcat底层用反射?tomcat底层设计不能和myservlet耦合,只有全限定名和myservlet有关(自己设定的),通用性。tomcat像管家中介(用户,tomcat软件【中介】,jdk的servlet)在第3步等客人,第4步客人来了找my,tomcat根据客人请求找到my的住址com....myservlet,把my叫醒进行service办公。
如上在web.xml中,myservlet01可以随便命名但要上下一致,my是资源位置,/my(因为在当前项目,http…8080可以省略,url)和com…myservlet(class,全限定名就是全类名就是包名加类名)对应关系。
package com. itheima02. life ;
import javax. servlet. * ;
import java. io. ioexception ; public class lifecycleservlet implements servlet { public lifecycleservlet ( ) { system . out
. println ( "lifecycleservlet" ) ; } @override public void init ( servletconfig servletconfig
) throws servletexception { system . out
. println ( "init" ) ; } @override public void service ( servletrequest servletrequest
, servletresponse servletresponse
) throws servletexception , ioexception { system . out
. println ( "service" ) ; } @override public void destroy ( ) { system . out
. println ( "destroy" ) ; } @override public servletconfig getservletconfig ( ) { system . out
. println ( "getservletconfig" ) ; return null ; } @override public string getservletinfo ( ) { system . out
. println ( "getservletinfo" ) ; return null ; }
}
//web.xml
< web-app xmlns = " http://java.sun.com/xml/ns/javaee" xmlns: xsi= " http://www.w3.org/2001/xmlschema-instance" xsi: schemalocation= " http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_1.xsd" version = " 3.1" > < servlet> < servlet-name> myservlet01
servlet-name > < servlet-class> com.itheima01.servlet.myservletservlet-class > servlet > < servlet-mapping> < servlet-name> myservlet01servlet-name > < url-pattern> /myurl-pattern > servlet-mapping > < servlet> < servlet-name> lifecycleservletservlet-name > < servlet-class> com.itheima02.life.lifecycleservletservlet-class > servlet > < servlet-mapping> < servlet-name> lifecycleservletservlet-name > < url-pattern> /lifeurl-pattern > servlet-mapping > < servlet> < servlet-name> goodservletservlet-name > < servlet-class> com.itheima03.good.goodservletservlet-class > servlet > < servlet-mapping> < servlet-name> goodservletservlet-name > < url-pattern> /goodurl-pattern > servlet-mapping > < servlet> < servlet-name> betterservletservlet-name > < servlet-class> com.itheima03.good.betterservletservlet-class > servlet > < servlet-mapping> < servlet-name> betterservletservlet-name > < url-pattern> /betterurl-pattern > servlet-mapping >
web-app >
如下默认调用空参构造创建实例(第一行)。
只留一个接口叫适配器设计模式。将service方法根据不同请求方式分化出两个不同方法。
package com. itheima03. good ;
import javax. servlet. genericservlet ;
import javax. servlet. servletexception ;
import javax. servlet. servletrequest ;
import javax. servlet. servletresponse ;
import javax. servlet. http. httpservletrequest ;
import javax. servlet. http. httpservletresponse ;
import java. io. ioexception ; public class goodservlet extends genericservlet {
@override public void service ( servletrequest servletrequest, servletresponse servletresponse) throws servletexception , ioexception { system . out. println ( "goodservlet" ) ; httpservletrequest request = ( httpservletrequest ) servletrequest; httpservletresponse response = ( httpservletresponse ) servletresponse; service2 ( request, response) ; } private void service2 ( httpservletrequest request, httpservletresponse response) { string method = request. getmethod ( ) ; if ( "get" . equals ( method) ) { doget ( request, response) ; } else if ( "post" . equals ( method) ) { dopost ( request, response) ; } } private void dopost ( httpservletrequest request, httpservletresponse response) { } private void doget ( httpservletrequest request, httpservletresponse response) { }
}
浏览器:localhost:8080/good,网页无显示,控制台打印goodservlet。
package com. itheima03. good ;
import javax. servlet. servletexception ;
import javax. servlet. http. httpservlet ;
import javax. servlet. http. httpservletrequest ;
import javax. servlet. http. httpservletresponse ;
import java. io. ioexception ; public class betterservlet extends httpservlet { @override protected void dopost ( httpservletrequest req, httpservletresponse resp) throws servletexception , ioexception { doget ( req, resp) ; } @override protected void doget ( httpservletrequest req, httpservletresponse resp) throws servletexception , ioexception { }
}
如上解决了前二个问题。如下解决第三个问题,不用配置web.xml。
package com. itheima03. good ;
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 ; @webservlet ( urlpatterns = "/best" )
public class bestservlet extends httpservlet { @override protected void dopost ( httpservletrequest req, httpservletresponse resp) throws servletexception , ioexception { doget ( req, resp) ; } @override protected void doget ( httpservletrequest req, httpservletresponse resp) throws servletexception , ioexception { system . out. println ( "被访问了" ) ; }
}
如下把上面bestservlet类设置为模板,使用:选中包(包下)-new-servlet。 如上模板如下:
#if ( ${ package_name} && ${ package_name} != "" ) package ${ package_name} ; #end
#parse ( "file header.java" ) @javax.servlet.annotation.webservlet ( urlpatterns = "/${entity_name}" )
public class ${ class_name } extends javax. servlet. http. httpservlet { @override protected void dopost ( javax. servlet. http. httpservletrequest request, javax. servlet. http. httpservletresponse response) throws javax. servlet. servletexception, java. io. ioexception { doget ( request, response) ; } @override protected void doget ( javax. servlet. http. httpservletrequest request, javax. servlet. http. httpservletresponse response) throws javax. servlet. servletexception, java. io. ioexception { }
}
http的request对象:由tomcat创建,并且里面数据由tomcat set进去,我们只需要get出来。 file-new-project-java enterprise。
doctype html >
< html lang = " en" >
< head> < meta charset = " utf-8" > < title> titletitle >
head >
< body> < h1> get请求方式h1 > < form action = " http://localhost:8080/myservlet" method = " get" > < input type = " text" placeholder = " 请输入用户名" name = " name" > < br> < input type = " text" placeholder = " 请输入密码" name = " pwd" > < br> < input type = " submit" > form > < h1> post请求方式h1 > < form action = " /myservlet" method = " post" > < input type = " text" placeholder = " 请输入用户名" name = " name" > < br> < input type = " text" placeholder = " 请输入密码" name = " pwd" > < br> < input type = " submit" > form >
body >
html >
如上html文件建在web文件夹路径下,如下创建的是servlet的模板。
package com. itheima01. http ;
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. util. * ;
@webservlet ( urlpatterns = "/myservlet" )
public class myservlet extends httpservlet { @override protected void dopost ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { doget ( request, response) ; } @override protected void doget ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception {
string agent = request. getheader ( "user-agent" ) ; string referer = request. getheader ( "referer" ) ; system . out. println ( agent) ; system . out. println ( referer) ; hashmap < string , string > map = new hashmap < > ( ) ; iterator < string > it = map. keyset ( ) . iterator ( ) ; while ( it. hasnext ( ) ) { string key = it. next ( ) ; string value = map. get ( key) ; } enumeration < string > it2 = request. getheadernames ( ) ; while ( it2. hasmoreelements ( ) ) { string name = it2. nextelement ( ) ; string value = request. getheader ( name) ; system . out. println ( name "->" value) ; } } private void line ( httpservletrequest request) { string method = request. getmethod ( ) ; stringbuffer url = request. getrequesturl ( ) ; string protocol = request. getprotocol ( ) ; string remoteaddr = request. getremoteaddr ( ) ; system . out. println ( method) ; system . out. println ( url. tostring ( ) ) ; system . out. println ( protocol) ; system . out. println ( "访问者的ip:" remoteaddr) ; }
}
如上浏览器网址中…报文.html是referer中的上一次访问页面,因为已跳转到如下下个页面。 如下请求行中的请求url省略了如上的http://localhost:8080。cookie是浏览器缓存的一种。get请求中最后一行不是请求体,是谷歌浏览器工具抓包进行的参数强调渲染。
doctype html >
< html lang = " en" >
< head> < meta charset = " utf-8" > < title> titletitle >
head >
< body> < h1> get请求h1 > < form action = " /paramservlet" method = " get" > 用户名: < input type = " text" name = " name" > < br> 密码: < input type = " password" name = " pwd" > < br> 性别: < input type = " radio" name = " gender" value = " boy" > 男< input type = " radio" name = " gender" value = " girl" > 女< br> 爱好:< input type = " checkbox" name = " hobby" value = " smoke" > 抽烟< input type = " checkbox" name = " hobby" value = " drink" > 喝酒< input type = " checkbox" name = " hobby" value = " firehead" > 烫头 < br> < input type = " submit" > form > < h1> post请求h1 > < form action = " /paramservlet" method = " post" > 用户名: < input type = " text" name = " name" > < br> 密码: < input type = " password" name = " pwd" > < br> 性别: < input type = " radio" name = " gender" value = " boy" > < input type = " radio" name = " gender" value = " girl" > < br> 爱好:< input type = " checkbox" name = " hobby" value = " smoke" > 抽烟< input type = " checkbox" name = " hobby" value = " drink" > 喝酒< input type = " checkbox" name = " hobby" value = " firehead" > 烫头 < br> < input type = " submit" > form >
body >
html >
package com. itheima02. param ;
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. util. arrays ;
import java. util. map ;
import java. util. set ;
@webservlet ( urlpatterns = "/paramservlet" )
public class paramservlet extends httpservlet { @override protected void dopost ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { request. setcharacterencoding ( "utf-8" ) ; doget ( request, response) ; } @override protected void doget ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception {
string name = request. getparameter ( "name" ) ; string pwd = request. getparameter ( "pwd" ) ; string gender = request. getparameter ( "gender" ) ; string [ ] hobbies = request. getparametervalues ( "hobby" ) ; system . out. println ( name"," pwd "," gender "," arrays . tostring ( hobbies) ) ; map < string , string [ ] > map = request. getparametermap ( ) ;
set < string > set = map. keyset ( ) ; for ( string key : set) { string [ ] value = map. get ( key) ;
} }
}
如下选中的是map遍历出来的结果和下面第一行一样。
request表面上获取请求数据,还有快递员身份。 如下forward方法中request是邮件及内容,response是回应权限。a模块指myservlet,b模块指zhouservlet。一次请求链如下只有一去一回。
doctype html >
< html lang = " en" >
< head> < meta charset = " utf-8" > < title> titletitle >
head >
< body> 人事< a href = " /shiservlet?msg=有人来面试" > 发送邮件a >
body >
html >
package com. itheima03. transfer ;
import javax. servlet. requestdispatcher ;
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 ; @webservlet ( urlpatterns = "/shiservlet" )
public class shiservlet extends httpservlet { @override protected void dopost ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { doget ( request, response) ; } @override protected void doget ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { string msg = request. getparameter ( "msg" ) ; system . out. println ( "shi:" msg) ; system . out. println ( "shi:我现在没空,叫周楠同学帮我面试" ) ; request. setattribute ( "extra" , "比我帅的不要~~" ) ;
request. getrequestdispatcher ( "/zhouservlet" ) . forward ( request, response) ; }
}
package com. itheima03. transfer ;
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 ; @webservlet ( urlpatterns = "/zhouservlet" )
public class zhouservlet extends httpservlet { @override protected void dopost ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { doget ( request, response) ; } @override protected void doget ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { string msg = request. getparameter ( "msg" ) ; system . out. println ( "zhou:" msg) ; object extra = request. getattribute ( "extra" ) ; system . out. println ( "zhou:" extra) ; system . out. println ( "周楠同学放下手机,难得做点事情: 面试" ) ; response. getwriter ( ) . print ( "this boy is ok,about 3k~~" ) ; }
}
file - new - project - java enterprise。 如下想到junit包失效。
package com. itheima. utils ;
import com. mchange. v2. c3p0. combopooleddatasource ;
import org. springframework. jdbc. core. jdbctemplate ; public class jdbcutil { private static combopooleddatasource ds = new combopooleddatasource ( ) ; public static jdbctemplate gettemplate ( ) { jdbctemplate template = new jdbctemplate ( ds) ; return template; }
}
package com. itheima. login ;
import com. itheima. bean. user ;
import com. itheima. utils. jdbcutil ;
import com. mchange. v2. c3p0. combopooleddatasource ;
import org. springframework. dao. dataaccessexception ;
import org. springframework. jdbc. core. beanpropertyrowmapper ;
import org. springframework. jdbc. core. jdbctemplate ;
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 ; @webservlet ( urlpatterns = "/loginservlet" )
public class loginservlet extends httpservlet { @override protected void dopost ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { doget ( request, response) ; } @override protected void doget ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { string username = request. getparameter ( "username" ) ; string password = request. getparameter ( "password" ) ; string sql = "select * from user where username = ? and password = ?" ; jdbctemplate template = jdbcutil . gettemplate ( ) ; user user = template. queryforobject ( sql, new beanpropertyrowmapper < > ( user . class ) , username, password) ; if ( user != null ) { request. getrequestdispatcher ( "/success.html" ) . forward ( request, response) ; } else { request. getrequestdispatcher ( "/error.html" ) . forward ( request, response) ; } }
}
package com. itheima. bean ; public class user { private integer id; private string username; private string password; @override public string tostring ( ) { return "user{" "id=" id ", username='" username '\'' ", password='" password '\'' '}' ; } public integer getid ( ) { return id; } public void setid ( integer id) { this . id = id; } public string getusername ( ) { return username; } public void setusername ( string username) { this . username = username; } public string getpassword ( ) { return password; } public void setpassword ( string password) { this . password = password; }
}
//login.html
doctype html >
< html lang = " en" >
< head> < meta charset = " utf-8" > < meta http-equiv = " x-ua-compatible" content = " ie=edge" > < meta name = " viewport" content = " width=device-width, initial-scale=1" > < title> 登录页面title > < link href = " css/bootstrap.min.css" rel = " stylesheet" > < link href = " css/login.css" rel = " stylesheet" > < script src = " js/jquery.js" > script > < script src = " js/bootstrap.js" > script >
head >
< body> < div class = " container text-center" > < form class = " form-signin" action = " /loginservlet" > < h2 class = " form-signin-heading" > 登录页面h2 > < input type = " text" name = " username" class = " form-control" placeholder = " 用户名" required autofocus > < input type = " password" name = " password" class = " form-control" placeholder = " 密码" required > < button class = " btn btn-lg btn-primary btn-block" type = " submit" > 登录button > form > div >
body >
html >
package com. itheima01. http ;
import javax. servlet. servletexception ;
import javax. servlet. servletoutputstream ;
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. printwriter ; @webservlet ( urlpatterns = "/myservlet" )
public class myservlet extends httpservlet { @override protected void dopost ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { doget ( request, response) ; } @override protected void doget ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { system . out. println ( "服务器被访问了" ) ;
response. setstatus ( 302 ) ;
}
}
请求报文分get和post两种,在响应报文里看到了非servlet设置的内容,那就是tomcat设置的。将前端改为href=“/myservlet123”,则会出现404。servlet关了,tomcat自动设置为500状态码。
如下体中hello response长度一共为content-lenth为14,头中gmt格林位置需 8小时为东八区北京时间。如下行和头都是tomcat默认自动设置,体是我们servlet手动设置。想修改状态码,需要在servlet(对我们暴露的小程序)中修改。 如下响应头分四种(蓝色字),如下简易api等同上行。 如下点击后,5秒后跳转到百度。
package com. itheima02. header ;
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 ; @webservlet ( urlpatterns = "/refreshandtypeservlet" )
public class refreshandtypeservlet extends httpservlet { @override protected void dopost ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { doget ( request, response) ; } @override protected void doget ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { system . out. println ( "服务器被访问了" ) ;
response. setcontenttype ( "text/html;charset=utf-8" ) ; response. getwriter ( ) . print ( "" ) ; }
}
因为重定向不是一次请求链,所以超过域对象,不能用request传递数据。zhouservlet收不到人事的msg,能收到shiservlet的response.setheader(…/zhouservlet?msg=…)。
package com. itheima03. redirect ;
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 ; @webservlet ( urlpatterns = "/shiservlet" )
public class shiservlet extends httpservlet { @override protected void dopost ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { request. setcharacterencoding ( "utf-8" ) ; doget ( request, response) ; } @override protected void doget ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { string msg = request. getparameter ( "msg" ) ; system . out. println ( "shi:" msg) ; response. setstatus ( 302 ) ; response. setheader ( "location" , "/zhouservlet?msg=xx" ) ; }
}
package com. itheima03. redirect ;
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 ; @webservlet ( urlpatterns = "/zhouservlet" )
public class zhouservlet extends httpservlet { @override protected void dopost ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { doget ( request, response) ; } @override protected void doget ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { string msg = request. getparameter ( "msg" ) ; system . out. println ( "zhou:" msg) ; system . out. println ( "这个哥们还不错" ) ; response. setcontenttype ( "text/html;charset=utf-8" ) ; response. getwriter ( ) . print ( "这个哥们还可以,大约3k" ) ; }
}
如下浏览器f12,第二行没有带参数,拿不到msg。 前面登陆成功请求转发到成功.html页面,失败.html…。用请求转发不合适,因为没有传递数据,应该用重定向。请求转发一次请求,地址指向第一次访问位置如下(网址阅读性太差,实际页面应该为localhost:8080/success.html)。 如下红框用重定向,登陆失败逻辑错误,之后再改。 如下重定向,第一次请求收到302。
首先服务器要有文件,在网络编程做过文件上传。在servlet里写代码覆盖tomcat默认设置。
//02文件下载.html
< html lang = " en" >
< head> < meta charset = " utf-8" > < title> titletitle >
head >
< body> < a href = " /downloadservlet?file=1.zip" > 1.zipa > < br> < a href = " /downloadservlet?file=2.exe" > 2.exea > < br> < a href = " /downloadservlet?file=3.txt" > 3.txta > < br> < a href = " /downloadservlet?file=4.jpg" > 4.jpga > < br>
body >
html >
package com. itheima04. down ;
import javax. servlet. servletcontext ;
import javax. servlet. servletexception ;
import javax. servlet. servletoutputstream ;
import javax. servlet. annotation. webservlet ;
import javax. servlet. http. httpservlet ;
import javax. servlet. http. httpservletrequest ;
import javax. servlet. http. httpservletresponse ;
import java. io. fileinputstream ;
import java. io. ioexception ; @webservlet ( urlpatterns = "/downloadservlet" )
public class downloadservlet extends httpservlet { @override protected void dopost ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { doget ( request, response) ; } @override protected void doget ( httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { string file = request. getparameter ( "file" ) ; response. setheader ( "content-disposition" , "attachment;filename=x" file) ;
servletcontext servletcontext = request. getservletcontext ( ) ; string realpath = servletcontext. getrealpath ( "dir/" file) ; fileinputstream fis = new fileinputstream ( realpath) ; servletoutputstream os = response. getoutputstream ( ) ; int length; byte [ ] buffer = new byte [ 1024 ] ; while ( ( length = fis. read ( buffer) ) != - 1 ) { os. write ( buffer, 0 , length) ; } fis. close ( ) ; os. close ( ) ; system . out. println ( "文件下载完毕" ) ; }
}
响应体交给浏览器,浏览器自动下载。 linux没有盘符,所以不能用绝对路径,不好部署。如下artifacts指整个项目,运行的是如下蓝字编译的内容(黄色文件夹),只要获得web_war_exploded文件夹就能得到其他文件相对路径。用serlvetcontext = web_war_exploded文件夹路径(exploded部署意思,如下红线),如下右边servletcontext是生命周期最长的域对象,所以可以将数据传到服务器任何地方。
request除了作为域对象还有本职工作即获取请求数据,servletcontext很像连接池(连接池一个就够),服务器没关,连接池不消毁。servletcontext生命周期和web应用一样长,所以也叫application应用,环境参数如相对路径。request和session两个域对象用的最多,pagecontext太短,servletcontext太长。重定向不能通过request传递数据,可以用servletcontext传递数据,但是一般也不这么做,servletcontext占用内存大。 上图灰色web-inf文件夹中添加lib文件夹里放druid-1.0.9.jar包并右击add as library【mysql3】,点乌龟运行tomcat后,会生成黄色lib文件夹。黄色classes文件夹里代码不能直接访问,需用web.xml或用注解建立映射关系访问:web-inf下是浏览器无法直接访问,通过建立映射,url访问,由tomcat访问。 热更新不做编译,新增类必须停下来。
与50位技术专家面对面 20年技术见证,附赠技术全景图
总结
以上是凯发k8官方网 为你收集整理的【java12】tomcatservlet(nginx,web.xml,生命周期,适配器优化),requestresponse(请求转发,登陆案例(1),重定向,文件下载) 的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发k8官方网 网站内容还不错,欢迎将凯发k8官方网 推荐给好友。