Java Servlet :是用Java编写的服务器端程序。其主要功能在于交互式地浏览和修改数据,生成动态Web内容。狭义的Servlet是指Java语言实现的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者。
Servlet运行于支持Java的应用服务器中。从实现上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Servlet只用来扩展基于HTTP协议的Web服务器。
工作流程图:
一,使用xml配置的方式
1.HelloWrold
HelloWorld.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package com.devnp.servlet;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override protected void doGet(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException { servletResponse.setContentType("text/html;charset=UTF-8"); PrintWriter out = servletResponse.getWriter(); try { out.println("<!DOCTYPE html>"); out.println("<html><head>"); out.println("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>"); out.println("<title>devnp.com</title></head>"); out.println("<body>"); out.println("
<h1>Hello, world! This is Servlet.</h1>
"); out.println("</body>"); out.println("</html>"); } finally { out.close(); } }
@Override protected void doPost(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException { }
@Override public void destroy() { super.destroy(); }
@Override public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); } }
|
web.xml
1 2 3 4 5 6 7 8 9
| <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>com.devnp.servlet.HelloWorld</servlet-class> </servlet>
<servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloWorld</url-pattern> </servlet-mapping>
|
访问:http://localhost:7002/JavaServlet/HelloWorld
2.表单提交的例子
form_input.html
[code lang=”html”]
first_form_input
Author:
Darren Du
License:
Copyright (c) 2019 MIT LICENSE