1.Java Web 模块构造
JSP文件和AXPX文件相似,门路和URL一一对应,都会被动静编译为独自class。Java Web和ASP.NET的外围是别离是Servlet和IHttpHandler接口,因而无论是根底的Page文件(JSP、ASPX)形式还是起初倒退的MVC形式(Spring MVC、ASP.NET MVC)都是基于外围接口的根底上再次封装和扩大(DispatcherServlet、MvcHandler)。
Javaweb我的项目实战教程在线观看(全程干货): https://www.bilibili.com/vide…
除JSP文件外,其余全副文件部署在利用目录的WEB-INF子目录下,WEB-INF目录能够认为是ASP.NET中将web.config文件、bin目录和App_结尾的运行时目录寄存在了一个对立的根目录中。
Java Web的配置文件web.xml也寄存在WEB-INF目录下,而ASP.NET的配置文件web.config个别间接寄存在利用目录下(ASP.NET其余目录同样能够有web.config文件)。ASP.NET将所有的援用和代码生成的dll都部署在bin中,而Java Web的援用jar和生成的class别离寄存在WEB-INF的子目录lib和classes中。
综上,相似ASP.NET中的web.config、bin、App_Data等,Java Web中的WEB-INF、web.xml、lib和classes是咱们必须理解和把握的。
|--Assembly Root
|---WEB-INF/
|--web.xml
|--lib/
|--classes/
- WEB-INF目录:Java Web文件的根目录。
- web.xml文件:配置文件(asp.net web.config)。
- lib目录:寄存类库文件(asp.net bin)。
- classes目录:寄存class文件(asp.net bin)。
2.Java Web我的项目的根本构造
Eclipse Dynamic Web Project我的项目
(1)能够配置须要编译的源码目录和输入目录,默认编译src目录下的源文件到build\classes目录下。
(2)能够配置WEB-INF的根目录,默认为WebContent。
(3)能够抉择是否生成默认web.xml文件。
咱们创立一个命名为DynamicWP的默认生成web.xml的Dynamic Web Proejct我的项目。文件构造如下:
|--DynamicWP
|--.settings/
|--build/
|--classes/
|--src/
|--WebContent/
|--META-INF/
|--MANIFEST.MF
|--WEB-INF/
|--web.xml
|--lib/
在Eclipse的我的项目资源管理器中DyanmicWP我的项目的视图如下:
|--DynamicWP
|--Deployment Desciptor
|--JAX-WS Web Services
|--Java Resources
|--JavaScript Resources
|--build
|--WebContent
|--META-INF/
|--MANIFEST.MF
|--WEB-INF/
|--web.xml
|--lib/
- .settings为Eclipse我的项目文件夹,寄存了Eslipse我的项目的各种配置。在Eclipse我的项目视图中不可见。
- src目录寄存源码。在Eclipse的我的项目视图中对应为Java Resources/src。
- build寄存编译后的文件。
- 能够在相似的\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\DynamicWP目录中查看运行时的文件构造。
3.Maven Web我的项目的根本构造
鉴于目前Java IDE泛滥并且都有肯定的拥泵,Eclipse的Java Web我的项目不具备可移植性。Maven即解决了我的项目构造的标准问题又提供了弱小援用解决等弱小的性能,在我的项目布局等方面曾经是目前事实上的规范。Maven我的项目的次要构造如下:
|--root
|--pom.xml
|--src/
|--main/
|--java/
|--resources/
|--webapp/
|--test/
|--java/
|--resources
|--target/
Eclipse中新建一个Maven web app我的项目。文件构造如下:
|--MavenWP
|--pom.xml
|--.project
|--.classpath
|--.settings/
|--src/
|--target/
|--classes/
|--m2e-wtp/
- pom.xml:maven我的项目配置文件。
- .project文件和.classpath文件以及.settings目录和target/m2e-wtp目录下的文件为Eclipse我的项目配置文件。
- src和target:maven规范我的项目目录。
Eclipse4.5.1中对应的我的项目资源管理视图
|--MavenWP
|--Deployment Desciptor/
|--Java Resources/
|--JavaScript Resources/
|--Deployed Resources/
|--src
|--target
|--pom.xml
- 默认创立的我的项目会增加一个index.jsp并报错:应用maven搜寻并增加servlet依赖更新后就能够失常运行。
- Java构建门路问题正告:应用maven搜寻并增加compiler插件并配置configuration节点更新就能够打消。
- 墙的问题配置maven镜像,我采纳的是http://maven.oschina.net/cont…。
- 默认创立的maven webapp短少的src/main/java、src/test/java和src/test/resources等目录须要本人手动增加。
- 批改.settings/org.eclipse.wst.common.project.facet.core.xml,更新<installed facet=”jst.web” version=”3.1″/>。
- web.xml根节点开始局部批改如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
Maven的配置文件pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.test</groupId>
<artifactId>MavenWP</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MavenWP Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>MavenWP</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
4.servlet根底
正如ASP.NET的外围是IHttpHandler一样,Java Web的外围是Servlet接口,位于javax.servlet命名空间中。Filter的概念能够参考ASP.NET的HttpModule,Servlet中的各种Listener能够参考ASP.NET HttpApplicaiton中相似的event。无论是Java还是.NET的Web技术,都是基于HTTP协定的具体实现。Java Web和ASP.NET中的一些外围项对应如下:
Servlet和ASP.NET的简化示意图:
用于简化web.xml配置的Servlet的注解(3.0开始反对,在ASP.NET中没有对应项):
(1)WebServlet:作用在javax.servlet.http.HttpServlet的实现类上。
(2)WebFilter:作用在javax.servlet.Filter的实现类上。
(3)WebListener:作用在Listener的实现类上(javax.servlet.ServletContextListener、javax.servlet.ServletContextAttributeListener、javax.servlet.ServletRequestListener、javax.servlet.ServletRequestAttributeListener、javax.servlet.http.HttpSessionListener、javax.servlet.http.HttpSessionAttributeListener)。
(4)WebInitParam:联合WebServlet和WebFilter注解用来配置属性。
(5)MultipartConfig:作用在javax.servlet.http.HttpServlet的实现类上。标注申请是mime/multipart类型。
用于Servlet容器初始化的ServletContainerInitializer(可实现无web.xml,3.0开始反对,可类比ASP.NET的Application_Start办法):
(1)Servlet容器启动时查找ServletContainerInitializer的实例。
(2)ServletContainerInitializer实例应用HandlesTypes标注一个或多个类型,Servlet容器将在启动时扫描classpath,获取这些类型的实例。
(3)Servlet容器在启动时调用ServletContainerInitializer实现类的onStartup办法,该办法能够获取HandlesTypes标注的所有类型对象。
5.自定义Session
Session在存储安全性要求较高的会话信息方面是必不可少的,Session当然相对不是用来存储用户登录状态的,但相似验证码等敏感信息却必须存储在Session中。对于分布式Web利用自定义Session反对独立的状态服务器或集群是必须的。
ASP.NET通过SessionStateModule通过配置文件配置理论的Session提供程序,Session提供程序实现了SessionStateStoreProviderBase,因而在ASP.NET中实现自定义Session是通过继承SessionStateStoreProviderBase实现,配置Session是通过Web.config。ASP.NET自定义session的代码参考github上的开源我的项目SQLiteSessionStateStore。
同理,Java Servlet中应用自定义Session通过Filter能够实现。因为不同的servlet容器对Session的实现不同,所以通用性最好的形式是继承HttpServletRequestWrapper重写getSession办法返回自定义的Session对象。Filter采纳了职责链模式(chain of responsibility),HttpServletRequestWrapper采纳了装璜模式(Decorator),能够通过《Head First 设计模式》浏览模式的相干内容。
(1)首先自定义继承HttpSession的MySession(为了便于演示,仅包装了容器的session并转发调用)。
import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
public class MySession implements HttpSession {
private HttpSession _containerSession;
public MySession(HttpSession session) {
this._containerSession = session;
}
@Override
public long getCreationTime() {
return this._containerSession.getCreationTime();
}
@Override
public String getId() {
return this._containerSession.getId();
}
@Override
public long getLastAccessedTime() {
return this._containerSession.getLastAccessedTime();
}
@Override
public ServletContext getServletContext() {
return this._containerSession.getServletContext();
}
@Override
public void setMaxInactiveInterval(int interval) {
this._containerSession.setMaxInactiveInterval(interval);
}
@Override
public int getMaxInactiveInterval() {
return this._containerSession.getMaxInactiveInterval();
}
@SuppressWarnings("deprecation")
@Override
public HttpSessionContext getSessionContext() {
return this._containerSession.getSessionContext();
}
@Override
public Object getAttribute(String name) {
return this._containerSession.getAttribute(name);
}
@SuppressWarnings("deprecation")
@Override
public Object getValue(String name) {
return this._containerSession.getValue(name);
}
@Override
public Enumeration<String> getAttributeNames() {
return this._containerSession.getAttributeNames();
}
@SuppressWarnings("deprecation")
@Override
public String[] getValueNames() {
return this._containerSession.getValueNames();
}
@Override
public void setAttribute(String name, Object value) {
this._containerSession.setAttribute(name, value);
}
@SuppressWarnings("deprecation")
@Override
public void putValue(String name, Object value) {
this._containerSession.putValue(name, value);
}
@Override
public void removeAttribute(String name) {
this._containerSession.removeAttribute(name);
}
@SuppressWarnings("deprecation")
@Override
public void removeValue(String name) {
this._containerSession.removeValue(name);
}
@Override
public void invalidate() {
this._containerSession.invalidate();
}
@Override
public boolean isNew() {
return this._containerSession.isNew();
}
}
(2)自定义继承HttpServletRequestWrapper的MyRequest
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;
public class MyRequest extends HttpServletRequestWrapper {
public MyRequest() {
super(null);
}
public MyRequest(HttpServletRequest request) {
super(request);
// TODO 主动生成的构造函数存根
}
@Override
public HttpSession getSession(boolean create) {
return new MySession(super.getSession(create));
}
@Override
public HttpSession getSession() {
return new MySession(super.getSession());
}
}
(3)自定义Filter将Request包装为MyRequest
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
@WebFilter("/*")
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO 主动生成的办法存根
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(new MyRequest((HttpServletRequest) request), response);
}
@Override
public void destroy() {
// TODO 主动生成的办法存根
}
}
通过注解配置了Filter,也能够通过原始的web.xml形式配置。
到这里就告一段落啦!!!上面是上文的小总结:
(1)配置文件:ASP.NET的web.config和Java的web.xml
(2)Web外围:ASP.NET的IHttpHandler接口和Java的Servlet接口
(3)拦截器:ASP.NET的HttpModule和Java的Filter
(4)应用程序事件:ASP.NET的HttpApplication event和Java的各种Listener
(5)启动器:ASP.NET的Application_Start和Java的ServletContainerInitializer
(6)援用治理:ASP.NET的Nuget和Java的Maven