GET http://localhost:8080/HttpServlet/hs 405
时间: 2023-09-25 11:11:39 浏览: 97
The HTTP status code 405 means "Method Not Allowed." This means that the request method (in this case, GET) is not supported for the requested resource (in this case, http://localhost:8080/HttpServlet/hs). This could be because the resource only allows certain HTTP methods (such as POST instead of GET), or because the server is not configured to handle the requested method.
相关问题
(6)新建一个类名为DemoServletContext02的Servlet,该Servlet路径映射为/context02 (7)分别在这两个Servlet中获取web应用上下文对象ServletContext并打印出来看它们是否是同一个对象 (8)给web应用配置初始化参数:{"loginid":"tom","loginpwd":"123"},并分别在上面两个Servlet中获取它们 (9)在DemoServletContext01中将键值对{"msg":"登录成功!"}存入ServletContext域对象中,在DemoServletContext02去获取上面这个键值对并输出 (10)分别在src、web及WEB-INF目录下新建三个配置文件jdbc.properties,文件内容分别为: jdbc01.driver=com.mysql.jdbc.Driver jdbc01.url=jdbc:mysql:///localhost/jdbc jdbc01.username=root jdbc01.password=123 jdbc02.driver=com.mysql.jdbc.Driver jdbc02.url=jdbc:mysql:///localhost/jdbc jdbc02.username=root jdbc02.password=123 jdbc03.driver=com.mysql.jdbc.Driver jdbc03.url=jdbc:mysql:///localhost/jdbc jdbc03.username=root jdbc03.password=123 (11)分别获取上面三个文件夹中的文件并输出其内容
6. 新建一个类名为 DemoServletContext02 的 Servlet,该 Servlet 路径映射为 /context02。
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext02 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("DemoServletContext02 context: " + context);
}
}
```
在 web.xml 中设置 Servlet 路径映射:
```xml
<servlet>
<servlet-name>DemoServletContext02</servlet-name>
<servlet-class>DemoServletContext02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoServletContext02</servlet-name>
<url-pattern>/context02</url-pattern>
</servlet-mapping>
```
7. 分别在这两个 Servlet 中获取 web 应用上下文对象 ServletContext 并打印出来看它们是否是同一个对象
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext01 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("DemoServletContext01 context: " + context);
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext02 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("DemoServletContext02 context: " + context);
}
}
```
在控制台中输出的结果应该是一样的,因为它们获取的是同一个 web 应用上下文对象。
8. 给 web 应用配置初始化参数:{"loginid":"tom","loginpwd":"123"},并分别在上面两个 Servlet 中获取它们。
在 web.xml 中添加以下配置:
```xml
<context-param>
<param-name>loginid</param-name>
<param-value>tom</param-value>
</context-param>
<context-param>
<param-name>loginpwd</param-name>
<param-value>123</param-value>
</context-param>
```
在 DemoServletContext01 和 DemoServletContext02 中获取这些初始化参数:
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext01 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
String loginid = context.getInitParameter("loginid");
String loginpwd = context.getInitParameter("loginpwd");
System.out.println("DemoServletContext01 loginid: " + loginid);
System.out.println("DemoServletContext01 loginpwd: " + loginpwd);
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext02 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
String loginid = context.getInitParameter("loginid");
String loginpwd = context.getInitParameter("loginpwd");
System.out.println("DemoServletContext02 loginid: " + loginid);
System.out.println("DemoServletContext02 loginpwd: " + loginpwd);
}
}
```
9. 在 DemoServletContext01 中将键值对 {"msg":"登录成功!"} 存入 ServletContext 域对象中,在 DemoServletContext02 去获取上面这个键值对并输出。
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext01 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
context.setAttribute("msg", "登录成功!");
System.out.println("DemoServletContext01: 键值对 {"msg":"登录成功!"} 已存入 ServletContext 域对象中。");
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext02 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
String msg = (String) context.getAttribute("msg");
System.out.println("DemoServletContext02: " + msg);
}
}
```
10. 分别在 src、web 及 WEB-INF 目录下新建三个配置文件 jdbc.properties,文件内容分别为:
jdbc01.driver=com.mysql.jdbc.Driver
jdbc01.url=jdbc:mysql:///localhost/jdbc
jdbc01.username=root
jdbc01.password=123
jdbc02.driver=com.mysql.jdbc.Driver
jdbc02.url=jdbc:mysql:///localhost/jdbc
jdbc02.username=root
jdbc02.password=123
jdbc03.driver=com.mysql.jdbc.Driver
jdbc03.url=jdbc:mysql:///localhost/jdbc
jdbc03.username=root
jdbc03.password=123
11. 分别获取上面三个文件夹中的文件并输出其内容。
```java
import java.io.InputStream;
import java.util.Properties;
public class DemoProperties {
public static void main(String[] args) {
// 读取 src 目录下的 jdbc.properties 文件
InputStream in1 = DemoProperties.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties prop1 = new Properties();
try {
prop1.load(in1);
System.out.println("jdbc01.driver: " + prop1.getProperty("jdbc01.driver"));
System.out.println("jdbc01.url: " + prop1.getProperty("jdbc01.url"));
System.out.println("jdbc01.username: " + prop1.getProperty("jdbc01.username"));
System.out.println("jdbc01.password: " + prop1.getProperty("jdbc01.password"));
} catch (IOException e) {
e.printStackTrace();
}
// 读取 web 目录下的 jdbc.properties 文件
InputStream in2 = DemoProperties.class.getClassLoader().getResourceAsStream("../jdbc.properties");
Properties prop2 = new Properties();
try {
prop2.load(in2);
System.out.println("jdbc02.driver: " + prop2.getProperty("jdbc02.driver"));
System.out.println("jdbc02.url: " + prop2.getProperty("jdbc02.url"));
System.out.println("jdbc02.username: " + prop2.getProperty("jdbc02.username"));
System.out.println("jdbc02.password: " + prop2.getProperty("jdbc02.password"));
} catch (IOException e) {
e.printStackTrace();
}
// 读取 WEB-INF 目录下的 jdbc.properties 文件
InputStream in3 = DemoProperties.class.getClassLoader().getResourceAsStream("../../jdbc.properties");
Properties prop3 = new Properties();
try {
prop3.load(in3);
System.out.println("jdbc03.driver: " + prop3.getProperty("jdbc03.driver"));
System.out.println("jdbc03.url: " + prop3.getProperty("jdbc03.url"));
System.out.println("jdbc03.username: " + prop3.getProperty("jdbc03.username"));
System.out.println("jdbc03.password: " + prop3.getProperty("jdbc03.password"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
Servlet写一个tomcat插件,基于IP,对于同一文件读取请求,例如http://192.168.0.23:8080/a.json,返回不同的内容给客户端
Servlet编写Tomcat插件并实现基于IP的内容定制,需要利用Servlet容器的特性以及Java的网络编程知识。这里是一个简单的示例,假设我们想要根据访问IP的不同提供不同的JSON数据:
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class IPBasedJsonServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ipAddress = request.getRemoteAddr(); // 获取客户端IP
String content;
if ("192.168.0.23".equals(ipAddress)) { // 如果是特定IP
content = "{\"data\":\"这是192.168.0.23的定制内容\"}";
} else {
content = "{\"data\":\"默认内容\"}"; // 否则提供默认内容
}
response.setContentType("application/json");
response.getWriter().write(content);
}
}
```
在这个例子中,`doGet`方法会处理HTTP GET请求。首先通过`HttpServletRequest`获取客户端的IP地址,然后根据预设条件(这里是硬编码的IP "192.168.0.23"),决定返回哪一种JSON内容。
为了部署这个Servlet到Tomcat,你需要将上述代码保存在一个名为`IPBasedJsonServlet.java`的文件中,并将其放在Web应用的`WEB-INF/classes`目录下。然后,在`web.xml`配置文件中添加Servlet和映射,如下所示:
```xml
<web-app>
<!-- ... -->
<servlet>
<servlet-name>IPBasedJsonServlet</servlet-name>
<class>com.yourpackage.IPBasedJsonServlet</class>
</servlet>
<servlet-mapping>
<servlet-name>IPBasedJsonServlet</servlet-name>
<url-pattern>/a.json</url-pattern>
</servlet-mapping>
<!-- ... -->
</web-app>
```
现在当你访问`http://localhost:8080/a.json`时,如果是192.168.0.23的请求会得到定制内容,其他IP则会获得默认内容。
阅读全文