package com.powernode.javaweb.servlet; import jakarta.servlet.Servlet; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebInitParam; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; //loadOnStartup启动时创建对象<loadOnStartup = 1> @WebServlet(name = "Hello",urlPatterns = {"/Hello1","/Hello2"},loadOnStartup = 1, initParams = {@WebInitParam(name = "user",value = "root"),@WebInitParam(name = "password",value = "abc123"), @WebInitParam(name = "driver",value = "com.mysql.jdbc.Driver")}) public class HelloServlet extends HttpServlet { public HelloServlet() { System.out.println("Hello启动!"); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取Servlet名字 String servletName = getServletName(); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println(servletName); out.println("<br>"); out.println(request.getServletPath()); } }
package com.powernode.javaweb.servlet; import jakarta.servlet.Servlet; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebInitParam; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; //loadOnStartup启动时创建对象<loadOnStartup = 1> @WebServlet(name = "Hello",urlPatterns = {"/Hello1","/Hello2"},loadOnStartup = 1, initParams = {@WebInitParam(name = "user",value = "root"),@WebInitParam(name = "password",value = "abc123"), @WebInitParam(name = "driver",value = "com.mysql.jdbc.Driver")}) public class HelloServlet extends HttpServlet { public HelloServlet() { System.out.println("Hello启动!"); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取Servlet名字 String servletName = getServletName(); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println(servletName); out.println("<br>"); out.println(request.getServletPath()); } }
JAVA-EE使用注解式开发代替xml配置------计算机网络经典
于 2023-10-12 11:43:28 首次发布