Skip to content

Commit

Permalink
update some demo
Browse files Browse the repository at this point in the history
  • Loading branch information
fa1c0n1 committed Apr 5, 2022
1 parent 9f0582a commit 36f3f1b
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 69 deletions.
9 changes: 6 additions & 3 deletions src/Demo5.java → src/me/mole/filter/FilterDemo.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package me.mole.filter;

import javax.servlet.*;
import java.io.IOException;

//自定义Filter示例
public class Demo5 implements Filter {
public class FilterDemo implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Demo5 Filter init...");
System.out.println("me.mole.filter.Demo5 Filter init...");
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
System.out.println("Demo5 Filter doFilter...");
System.out.println("me.mole.filter.Demo5 Filter doFilter...");
filterChain.doFilter(servletRequest, servletResponse);
}

@Override
public void destroy() {
System.out.println("me.mole.filter.Demo5 Filter destroy...");
}
}
32 changes: 32 additions & 0 deletions src/me/mole/listener/ListenerDemo2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.mole.listener;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletResponse;

public class ListenerDemo2 implements ServletRequestListener {
private HttpServletResponse response;

/**
* 如果按照正常的注册流程去注册的话,
* 自定义的Listener需要有无参构造方法.
* 否则tomcat启动会报错.
*/
public ListenerDemo2() {

}

public ListenerDemo2(HttpServletResponse response) {
this.response = response;
}

@Override
public void requestDestroyed(ServletRequestEvent sret) {
System.out.println("ListenerDemo requestDestroyed");
}

@Override
public void requestInitialized(ServletRequestEvent sret) {
System.out.println("ListenerDemo requestInitialized");
}
}
48 changes: 0 additions & 48 deletions src/me/mole/servlet/Demo6.java

This file was deleted.

49 changes: 49 additions & 0 deletions src/me/mole/servlet/ServletDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package me.mole.servlet;

import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

//自定义Servlet示例
public class ServletDemo extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
System.out.println("me.mole.servlet.ServletDemo doGet...");
// ServletOutputStream out = resp.getOutputStream();
// String cmds = req.getParameter("servletinjt");
// if (cmds != null) {
// InputStream in = Runtime.getRuntime().exec(cmds.split(" ")).getInputStream();
// StringBuilder sb = new StringBuilder();
// BufferedReader br = new BufferedReader(new InputStreamReader(in));
// String line = null;
// while((line = br.readLine()) != null) {
// sb.append(line).append("\n");
// }
// if (sb.length() > 0) {
// out.print("<pre>");
// out.write(sb.toString().getBytes());
// out.print("</pre>");
// out.flush();
// out.close();
// }
// }
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
37 changes: 20 additions & 17 deletions web/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
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_4_0.xsd"
version="4.0">

<!-- <filter>-->
<!-- <filter-name>Demo5</filter-name>-->
<!-- <filter-class>Demo5</filter-class>-->
<!-- </filter>-->
<!-- <filter-mapping>-->
<!-- <filter-name>Demo5</filter-name>-->
<!-- <url-pattern>/*</url-pattern>-->
<!-- </filter-mapping>-->

<!--<servlet>-->
<!-- <servlet-name>Demo6</servlet-name>-->
<!-- <servlet-class>me.mole.servlet.Demo6</servlet-class>-->
<!--</servlet>-->
<!--<servlet-mapping>-->
<!-- <servlet-name>Demo6</servlet-name>-->
<!-- <url-pattern>/servletmem/*</url-pattern>-->
<!--</servlet-mapping>-->
<filter>
<filter-name>filterdemo</filter-name>
<filter-class>me.mole.filter.FilterDemo</filter-class>
</filter>
<filter-mapping>
<filter-name>filterdemo</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>servletdemo</servlet-name>
<servlet-class>me.mole.servlet.ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletdemo</servlet-name>
<url-pattern>/servlet/servletdemo</url-pattern>
</servlet-mapping>

<listener>
<listener-class>me.mole.listener.ListenerDemo2</listener-class>
</listener>
</web-app>
2 changes: 1 addition & 1 deletion web/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
<title>$Title$</title>
</head>
<body>
$END$
hello
</body>
</html>

0 comments on commit 36f3f1b

Please sign in to comment.