下面是web.xml,servlet和jsp代码
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String userName = request.getParameter("userName");
PrintWriter out = response.getWriter();
out.println("hello "+userName+" how are you ?");
}
}
JSPCode:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
This is First Servlet
<form action="firstTest">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td> Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2"><input name = "sumbit "type="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
Web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Trial</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/firstTest</url-pattern>
</servlet-mapping>
</web-app>
当我提供URL http:localhost:8080 /试用时,Index.jsp即将到来,但是当我提供用户名和密码时,URL正在转为http:localhost:8080 / firstTest而不是http:localhost:8080 / Trial / firstTest我得到405错误&#34; Tomcat错误HTTP状态405 - 此方法不支持HTTP方法GET&#34;在我的代码中出了什么问题
答案 0 :(得分:3)
...错字
protected void doGost
更改为doPost或doGet ...
答案 1 :(得分:0)
在你的jsp代码表单标签中你没有指定方法名,默认情况下它使用GET方法,所以只需替换这段代码,我希望它可以工作:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("userName");
PrintWriter out = response.getWriter();
out.println("hello " + userName + " how are you ?");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
答案 2 :(得分:0)
在我们部署到服务器之前解决此类拼写错误
我们使用@override注释来测试方法是否是重载方法。什么时候你编译java它会抛出异常。实际上编译器会检查语法错误等。但是在这种情况下编译器处理的doGost()也是一种方法,通过在编译级别使用上面的注释,只有开发人员才能理解问题发生的位置。
因此。我的建议是@override用于重写方法。