Java Web series: Java Web basics and Java Web Basics
1. Java Web module structure
The JSP file is similar to the AXPX file, and the path and URL are one-to-one matched and will be dynamically compiled into a separate class. Java Web and ASP. the core of NET is Servlet and IHttpHandler interfaces. Therefore, whether it is the basic Page file (JSP, ASPX) method or the later developed MVC method (Spring MVC, ASP. net mvc) are re-encapsulated and extended based on core interfaces (DispatcherServlet and MvcHandler ).
In addition to JSP files, all other files are deployed in the WEB-INF subdirectory of the application directory, the WEB-INF directory can be considered ASP. NET will be web. the runtime directories starting with config file, bin directory, and App _ are stored in a unified root directory.
Java Web configuration file web. xml is also stored in the WEB-INF directory, while ASP. NET configuration file web. config is stored directly in the application directory (ASP.. NET other directories can also have web. config file ). ASP.. NET deploys all references and code-generated dll in the bin, the referenced jar and generated class of Java Web are stored in the lib and classes sub-directories of the WEB-INF respectively (refer to 1 ).
In summary, similar to web. config, bin, and App_Data in ASP. NET, we must understand and master Web-INF, WEB. xml, lib, and classes in Java web.
|--Assembly Root |---WEB-INF/ |--web.xml |--lib/ |--classes/
2. Basic Structure of Java Web Project [Eclipse Dynamic Web Project]
Eclipse Dynamic Web Project
(1) You can configure the source code directory and output directory to be compiled. By default, the source files under the src directory are compiled under the build \ classes directory.
(2) You can configure the root directory of the WEB-INF, the default is WebContent.
(3) You can choose whether to generate the default web. xml file.
Create a Dynamic web Proejct project named DynamicWP that generates Web. xml by default. The file structure is as follows:
|--DynamicWP |--.settings/ |--build/ |--classes/ |--src/ |--WebContent/ |--META-INF/ |--MANIFEST.MF |--WEB-INF/ |--web.xml |--lib/
In the project resource manager of Eclipse, the view of the DyanmicWP project is as follows:
|--DynamicWP |--Deployment Desciptor |--JAX-WS Web Services |--Java Resources |--JavaScript Resources |--build |--WebContent |--META-INF/ |--MANIFEST.MF |--WEB-INF/ |--web.xml |--lib/
3. Basic Structure of Maven Web Project
Currently, Java IDE is widely used and has a certain number of pumps, so the Java Web project of Eclipse is not portable. Maven solves the standardization problem of the project structure and provides powerful reference processing and other powerful functions. It is already a de facto standard in terms of project layout. The main structure of the Maven project is as follows (refer to 2 ):
|--root |--pom.xml |--src/ |--main/ |--java/ |--resources/ |--webapp/ |--test/ |--java/ |--resources |--target/
Create a Maven web app project in Eclipse. The file structure is as follows:
|--MavenWP |--pom.xml |--.project |--.classpath |--.settings/ |--src/ |--target/ |--classes/ |--m2e-wtp/
Project Resource Management View in Eclipse4.5.1
|--MavenWP |--Deployment Desciptor/ |--Java Resources/ |--JavaScript Resources/ |--Deployed Resources/ |--src |--target |--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 Basics
Just as IHttpHandler is the core of ASP. NET, the core of Java Web is Servlet interface, which is located in the javax. servlet namespace. For more information about Filter, see ASP. NET HttpModule. For more information about Listener in Servlet, see event similar to ASP. NET HttpApplicaiton. Both Java and. NET Web technologies are implemented based on the HTTP protocol. Some core items in Java Web and ASP. NET correspond to the following:
| |
Java Reference 3 |
. NET |
Remarks |
| Core |
Javax. servlet. Servlet |
System. Web. IHttpHandler |
|
| HTTP Request |
Javax. servlet. ServletRequest |
System. Web. HttpRequest |
|
| HTTP Response |
Javax. servlet. ServletResponse |
System. web. HttpResponse |
|
| Cookie |
Javax. servlet. http. Cookie |
System. Web. HttpCookie |
|
| Session |
Javax. servlet. http. HttpSession |
System. Web. HttpSessionState |
|
| Application |
Javax. servlet. ServletContext |
System. Web. HttpApplication |
|
| Begin Request |
Javax. servlet. Servlet. RequestDispatcher |
System. Web. HttpApplication. BeginRequest |
Event |
| Begin \ End Request |
Javax. servlet. Servlet. ServletRequestListener |
System. Web. HttpApplication. BeginRequest \ EndRequest |
Event |
| Filter |
Javax. servlet. Filter |
System. Web. IHttpModule |
|
| Application Event |
Javax. servlet. ServletContextListener |
System. Web. HttpApplication. Application_Start \ Application_End |
Method |
Servlet and ASP. NET simplification:
5. Custom Session
Sessions are essential for storing Session information with high security requirements. sessions are certainly not used to store user logon status, but sensitive information such as verification codes must be stored in sessions. It is required that custom sessions of distributed Web applications support independent State servers or clusters.
ASP. NET uses SessionStateModule to configure the actual Session provider through the configuration file. The Session provider implements SessionStateStoreProviderBase. in. NET, the custom Session is implemented by inheriting SessionStateStoreProviderBase, and the Session is configured through the Web. config. For ASP. NET custom session code, refer to the open-source project SQLiteSessionStateStore on github.
Similarly, the custom Session can be implemented in Java Servlet Through Filter. Because different servlet containers implement different sessions, the best universal method is to inherit from HttpServletRequestWrapper and override the getSession method to return custom Session objects. The Filter adopts the chain of responsibility mode, and the HttpServletRequestWrapper adopts the Decorator mode. You can use the Head First design mode to read the relevant content in the mode.
(1) first, customize the MySession that inherits HttpSession (for demonstration convenience, only the session of the container is encapsulated and the call is forwarded ).
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 ();}}View Code
(2) custom inherited MyRequest of HttpServletRequestWrapper
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 ); // The constructor stub automatically generated by TODO} @ Override public HttpSession getSession (boolean create) {return new MySession (super. getSession (create) ;}@ Override public HttpSession getSession () {return new MySession (super. getSession ());}}View Code
(3) The custom Filter wraps the Request as 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 {// method stub automatically generated by 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 () {// method stub automatically generated by TODO }}View Code
The Filter is configured through annotations, and can also be configured through the original web. xml method.
6. Reference
1. https://docs.oracle.com/javaee/7/tutorial/packaging003.htm
2. http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
3. https://docs.oracle.com/javaee/7/tutorial/webapp005.htm