JavaWeb Servlet
yyy言者 人气:01.servlet:定义:
接口
2.配置servlet:
public class HelloServlet extends HttpServlet {}
HttpServlet 固定的
IDEA配置v好了
类
package com.example.demo1; import java.io.*; import javax.servlet.http.*; import javax.servlet.annotation.*; @WebServlet(name = "helloServlet", value = "/hello-servlet") public class HelloServlet extends HttpServlet { private String message; public void init() { message = "Hello World!"; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); // Hello PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("<h1>" + message + "</h1>"); out.println("</body></html>"); } public void destroy() { } }
xml:
<servlet-name>demo1</servlet-name>//把类的名称配个短的文字
<servlet-class>com.example.demo3.HelloServlet</servlet-class>//类的名称(路径) //package com.example.demo3;可以利用包,来知道路径
<servlet-mapping> //映射 <servlet-name>demo1</servlet-name> // <url-pattern>/demo1</url-pattern> //将来能通过这个路径访问类 </servlet-mapping>
/servlet-class //标签
<servlet> <servlet-name>demo1</servlet-name> <servlet-class>com.example.demo3.HelloServlet</servlet-class> <load-on-startup>5</load-on-startup> </servlet> <servlet-mapping> <servlet-name>demo13</servlet-name> <url-pattern>demo13</url-pattern> </servlet-mapping>
3.通过: demo1访问
因为我设置了路径所以不同
我的是这个
http://localhost:8080/demo3_war_exploded/demo1
4.servlet:
5.servlet生命周期:
init(){}
// 在Servlet创建时,执行一次init(){}
service(){}
destroy(){} //服务器正常关闭时,执行一次:destroy(){}
加载全部内容