Eclipse 中的 Servlet:请求的资源 [/Register/] 不可用 [重复]

Posted

技术标签:

【中文标题】Eclipse 中的 Servlet:请求的资源 [/Register/] 不可用 [重复]【英文标题】:Servlet in Eclipse: The requested resource [/Register/] is not available [duplicate] 【发布时间】:2021-01-29 08:13:52 【问题描述】:

确切错误:请求的资源 [/Register/] 不可用。说明 源服务器没有找到目标资源的当前表示,或者不愿意透露存在。

我正在使用 tomcat 和 Eclipse。

代码如下:

RegisterDao.java

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class RegisterDao 
    
    private String dbUrl = "jdbc:mysql://localhost:3306/explorecalifornia";
    private String dbUname = "dbuser";
    private String dbPassword = "dbpassword";
    private String dbDriver = "com.mysql.cj.jdbc.Driver";
    
    public void loadDriver(String dbDriver)
    
        try 
            Class.forName(dbDriver);
         catch (ClassNotFoundException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
    
    
    public Connection getConnection()
    
        Connection con = null;
        try 
            con = DriverManager.getConnection(dbUrl, dbUname, dbPassword);
         catch (SQLException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
        return con;
    
    
    
    public String insert(Member member)
    
        loadDriver(dbDriver);
        Connection con = getConnection();
        String result = "Data entered successfully";
        String sql = "insert into member values(?,?,?,?)";
        
        PreparedStatement ps;
        try 
        ps = con.prepareStatement(sql);
        ps.setString(1, member.getUname());
        ps.setString(2, member.getPassword());
        ps.setString(3, member.getEmail());
        ps.setString(4, member.getPhone());
        ps.executeUpdate();
         catch (SQLException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = "Data not entered";
        
        return result;
    



Register.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/Register")
public class Register extends HttpServlet 
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() 
        super();
        // TODO Auto-generated constructor stub
    

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    
        String uname = request.getParameter("uname");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        
        Member member = new Member(uname, password, email, phone);
        
        RegisterDao rDao = new RegisterDao();
        String result = rDao.insert(member);
        response.getWriter().print(result);
        
    



web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Registration</display-name>
  <welcome-file-list>
    <welcome-file>memberRegistration.jsp</welcome-file>
    
  </welcome-file-list>
</web-app>

和 memberRegistration.jsp


  
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="memberRegistration" method="post">
<table>
<tr><td>User Name: </td><td><input type="text" name="uname"></td></tr>
<tr><td>Password: </td><td><input type="password" name="password"></td></tr>
<tr><td>Email: </td><td><input type="text" name="email"></td></tr>
<tr><td>phone: </td><td><input type="text" name="phone"></td></tr>
<tr><td></td><td><input type="submit" value="register"></td></tr>
</table>
</form>
</body>
</html>

【问题讨论】:

您的操作名称在 jsp 文件中是否正确。即这是“注册”而不是“会员注册”?你能提到文件的包结构吗? 我只是尝试将 jsp 中的操作更改为 memberRegistration 但它没有改变任何内容 应该是“注册”——你能告诉我你的java类所在的包名吗? 它们不在包中../default/src 添加为答案,有效吗? 【参考方案1】:

需要将servlet 类放在java 包中。您应该始终将可公开重用的 Java 类放在包中,否则它们对于包中的类(例如服务器本身)是不可见的。

【讨论】:

他们现在在一个包里,它仍然说Register is not found

以上是关于Eclipse 中的 Servlet:请求的资源 [/Register/] 不可用 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

刚刚用eclipse和lomboz调试servlet出错

Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”

Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”

Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”

Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”

Servlet 返回“HTTP 状态 404 请求的资源 (/servlet) 不可用”