本文主要是介绍JAVA---Servlet,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package servlet;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
*通过实现Servlet接口的方式创建Servlet
* @create 2022-04-21 20:52
*/
public class HelloServlet implements Servlet {
public HelloServlet(){
System.out.println("1 构造器方法");
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("2 init初始化方法");
//1.可以获取Servlet程序的别名servlet-name的值
System.out.println("HelloServlet程序的别名是:"+servletConfig.getServletName());
//2.获取初始化参数init-param
System.out.println("初始化参数username的值是:"+servletConfig.getInitParameter("username"));
System.out.println("初始化参数url的值是:"+servletConfig.getInitParameter("url"));
//3.获取ServletContext对象
System.out.println(servletConfig.getServletContext());
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("servlet is access");
//类型转换(因为HttpServletRequest有getMethod()方法)
HttpServletRequest httpServletRequest= (HttpServletRequest) servletRequest;
//获取请求的方式
String method = httpServletRequest.getMethod();
if("GET".equals(method)){
doGet();
} else if("POST".equals(method)){
doPost();
}
}
/*
做Get请求的操作
*/
public void doGet(){
System.out.println("get请求");
}
/*
做post请求的操作
*/
public void doPost(){
System.out.println("post请求");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
System.out.println("4. destroy销毁方法");
}
}
package servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 通过继承HttpServlet类的方式创建Servlet
* @create 2022-04-22 22:28
*/
public class HelloServlet2 extends HttpServlet {
/**
* 在get请求的时候调用
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("HelloServlet2 的doGet方法");
ServletConfig servletConfig = getServletConfig();
System.out.println(servletConfig);
//获取初始化参数init-param
System.out.println("初始化参数username的值是:"+servletConfig.getInitParameter("username"));
System.out.println("初始化参数url的值是:"+servletConfig.getInitParameter("url"));
}
/**
* 处理post请求的时候调用
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("HelloServlet2的doPost方法");
}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("重写了init初始化方法,做了一些工作");
}
}
package servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
*
* @create 2022-04-23 10:54
*/
public class ContextServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取web.xml中配置的上下文参数context-param
ServletContext context = getServletConfig().getServletContext();
System.out.println("context-param参数username的值是:"+context.getInitParameter("username"));
System.out.println("context-param参数password的值是:"+context.getInitParameter("password"));
//获取当前的工程路径,格式:/工程路径
System.out.println("当前工程路径"+context.getContextPath());
//获取工程部署后在服务器硬盘上的绝对路径
/*
/ 斜杠被服务器解析地址为:http://ip:port/工程名/ 映射到idea代码的web目录
*/
System.out.println("工程部署的路径是:"+context.getRealPath("/"));
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet");
}
}
package servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @create 2022-04-23 11:25
*/
public class ContextServlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取servletContext对象
ServletContext servletContext = getServletContext();
System.out.println(servletContext);
System.out.println("保存之前:"+servletContext.getAttribute("key1"));
servletContext.setAttribute("key1","value1");
System.out.println("保存之后:"+servletContext.getAttribute("key1"));
}
}
package servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @create 2022-04-23 11:40
*/
public class ContextServlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
System.out.println(servletContext);
System.out.println("ContextServlet2获取ContextServlet1中的key1的值为:"+servletContext.getAttribute("key1"));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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_3_1.xsd"
version="3.1">
<!--context-param是上下文参数(属于整个web工程)-->
<context-param>
<param-name>username</param-name>
<param-value>context</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</context-param>
<!--servlet标签给Tomcat配置Servlet程序 -->
<servlet>
<!--servlet-name标签给servlet程序起一个别名(一般是类名)-->
<servlet-name>HelloServlet</servlet-name>
<!--servlet-class是Servlet程序的全类名-->
<servlet-class>servlet.HelloServlet</servlet-class>
<!--init-param是初始化参数-->
<init-param>
<!--参数名-->
<param-name>username</param-name>
<!--是参数值-->
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</init-param>
</servlet>
<!--servlet-mapping标签给servlet程序配置访问地址-->
<servlet-mapping>
<!--servlet-name标签的作用是告诉服务器,当前配置的地址要给哪个Servlet程序使用-->
<servlet-name>HelloServlet</servlet-name>
<!--
url-pattern标签配置访问地址
/斜杠在服务器解析的时候,表示地址为:http://ip:port/工程路径
/hello表示地址为:http://ip:port/工程路径/hello
-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>HelloServlet2</servlet-name>
<servlet-class>servlet.HelloServlet2</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>this is HelloServlet2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet2</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ContextServlet</servlet-name>
<servlet-class>servlet.ContextServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextServlet</servlet-name>
<url-pattern>/context</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ContextServlet1</servlet-name>
<servlet-class>servlet.ContextServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextServlet1</servlet-name>
<url-pattern>/context1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ContextServlet2</servlet-name>
<servlet-class>servlet.ContextServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextServlet2</servlet-name>
<url-pattern>/context2</url-pattern>
</servlet-mapping>
</web-app>
这篇关于JAVA---Servlet的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!