gpt4 book ai didi

java - 缺少 tomcat 资源,servlet 未运行

转载 作者:行者123 更新时间:2023-11-28 22:43:15 25 4
gpt4 key购买 nike

import javax.servlet.*;
import java.io.*;

public class MyServlet implements Servlet
{

public void init(ServletConfig con) {}

public void service(ServletRequest req, ServletResponse res) throws IOException,ServletException
{

res.setContentType("text/html");
PrintWriter out=res.getWriter();
String s="blah";
String s1="blah";
out.println("<html><body>");

if((s.equals(req.getParameter("firstname")))&&(s1.equals(req.getParameter("pwd"))))

out.println("passwords match");

else
out.println("password and name combo does not match");

out.println("</body></html>");
}

public void destroy() {}

public ServletConfig getServletConfig() { return null;}
public String getServletInfo() { return null;}
}

这是我的带有 servlet 类的 java 文件。它以名称 MyServlet.java 保存,类文件也是如此。

这是 xml 文件:

<web-app>
<servlet>
<servlet-name>demoo</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demoo</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
</web-app>

我已将文件夹设为 WEB-INF,然后是类...WEB-INF 还包含.xml 文件

但是当我尝试运行 servlet 时,它说找不到资源ps- 我已经在寻找名称为 :demo 的 servlet

localhost:8081/s1/demo*s1 是 war 文件*

a html file in the war file seems to run fine on the server though.

*

最佳答案

使用以下代码,删除您的 web.xml,重新编译并部署您的网络应用

  import javax.servlet.*;
import java.io.*;
import javax.servlet.annotation.WebServlet;

@WebServlet(name = "MyServlet", urlPatterns = {"/demo"})
public class MyServlet implements Servlet {

public void init(ServletConfig con) {
}

public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException {

res.setContentType("text/html");
PrintWriter out = res.getWriter();
String s = "blah";
String s1 = "blah";
out.println("<html><body>");

if ((s.equals(req.getParameter("firstname"))) && (s1.equals(req.getParameter("lastname")))) {
out.println("passwords match");
} else {
out.println("password and name combo does not match");
}

out.println("</body></html>");
}

public void destroy() {
}

public ServletConfig getServletConfig() {
return null;
}

public String getServletInfo() {
return null;
}
}

关于java - 缺少 tomcat 资源,servlet 未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19469232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com