Code Address: https://github.com/iyuanyb/webserver
Achieved
-
Static, dynamic resource acquisition;
-
Cookie, Session, long HTTP connection, and the timing and clear the HTTP Session long link;
-
Spring MVC annotation similar programming, such as
@RequestMapping
@RequestParam
the like, data may be obtained from the process according to the parameter reception names, objects can be passed, and also supports cascading attributes, such as:// GET /page?pageSize=10&pageNum=1 HTTP/1.1 @RequestMapping("/page") String page(@RequestParam(value="pageSize", defaultValue="10") Integer pageSize, Integer pageNum) {...} /** * POST /login HTTP/1.1 * ... * user.name=admin&user.passwd=admin&user.data.val=ok * ******* * User 类:String name; String passwd; Data data; * Data type: String val; */ @RequestMapping("/login", method = HttpMethod.POST) String login(User user) {...}
-
Method returns a string that represents the path template, the template is achieved using a positive, only from
request
andsession
getting a property value field, such as${request.user.id}
; -
Logging (built using java.util.logging logger, customized log format): related to the operation log server (server-n.log), HTTP requests the log (access-n.log).
API Summary:
@Controller @RequestMapping @RequestParam @RequestHeader @CookieValue HttpRequest HttpResponse HttpSession Cookie HttpMethod
Instructions
Import jar package (see release) related to the use of notes, also need to create a webapp directory in the classpath, it represents the root path of static web resources. Then in the main class of main
method calls BootStrap.run()
, because the scan controller is implemented by traversing the directory, so the project does not support the package, must be published in the form of class files. Server-config.properties parameters need to provide a profile to the classpath, contains the following configuration items:
# Server port PORT=80 Path # log files are stored LOG_FILE_STORAGE_PATH=E:\\ # Connection expiration time in milliseconds CONNECTION_EXPIRY_TIME=30000 # Clean up period expired connected milliseconds CONNECTION_CLEANING_CYCLE=30000 # Session expiration time in milliseconds SESSION_EXPIRY_TIME=30000 # Clean up expired Session of the cycle, in milliseconds SESSION_CLEANING_CYCLE=30000 # The number of threads to listen for client reading event POLLER_THREAD_COUNT=2 # Processing specific requests thread pool size REQUEST_PROCESSOR_THREAD_COUNT=4
Show
- Project structure
-
EchoController.java
package com.test; // 导包 omitted ... @Controller // Only @Controller mark will be considered as controller // @RequestMapping supports the use of annotations in the class, public class EchoController { // thread-safe private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); @RequestMapping ( "/ echo") // mapped to "/ echo" public String echo(HttpRequest request, @RequestParam(value = "msg", defaultValue = "输入为空") String msg) { LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(request.getSession().getLastAccessedTime() / 1000, 0, ZoneOffset.ofHours(8)); request.setAttribute("lastAccessedTime", localDateTime.format(formatter)); request.setAttribute("msg", msg); return "test.html"; // to render the template path (classpath: webapp / test.html) } public static void main(String[] args) { BootStrap.run(); } }
-
test.html
<!DOCTYPE> <html lang="en"> <head> <title>Test</title> </head> <body> <p>Echo: ${request.msg}</p> <p>Last Accessed Time: ${request.lastAccessedTime}</p> <p><img src="img/girl.jpg" alt="girl" width="320" height="480"/></p> </body> </html>
-
effect
-
Journal