所请求的资源 [projectname/servlet] 不可用。

huangapple go评论83阅读模式
英文:

The requested resource [projectname/servlet] is not available

问题

这是我的项目结构:
[1]: https://i.stack.imgur.com/ootY1.png

我的问题是,每当我在服务器上运行项目时,就会出现HTTP状态404错误。这是服务器返回的错误信息:

请求的资源[/Doctor_Appointment_Application/Regis]不可用

我正在使用注解来注册我的Servlet,我的web.xml文件中只有一个欢迎文件。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0"
    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_4_0.xsd">
    <display-name>User Login</display-name>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/JSP/login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

我的Register Servlet:

package com.servlet;

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;

import com.bean.RegisBean;
import com.dao.RegisDao;

@WebServlet("/Regis")
public class Regis extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Regis() {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // ...(后续代码略)
    }
}

编辑 1:

Register.jsp:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Registration Page</title>
<style type="text/css">
<%-- <%@include file="/CSS/register.css"%> --%>
<%@include file="/CSS/regis.css"%>
</style>
<script src='https://kit.fontawesome.com/a076d05399.js'></script>
<script>
function validate() {
    var password = document.form.password.value;
    var confpassword = document.form.confpassword.value;

    if (password != confpassword) {
        alert("Confirm Password should match with the Password");
        return false;
    }
}
</script>
</head>
<body>
<form name="form" action="<%=request.getContextPath() %>/Regis" method="post" onsubmit="return validate()">
    <!-- 表单输入元素 -->
    <!-- ...(后续代码略) -->
    <input type="submit" value="Register">
</form>
</body>
</html>

编辑 2:

我刚刚尝试进行了一个实验,我创建了另一个JSP文件和一个Servlet来检查问题是否出现在我的JSP和Servlet文件中。所以同样的事情发生了,JSP文件成功运行,但服务器在提交表单后找不到Servlet。

任何帮助都将不胜感激。

英文:

This is my structure of the project:-
[1]: https://i.stack.imgur.com/ootY1.png

My problem is that whenever I am running my project on the server, there is an HTTP Status 404 Error.
This is the error I am getting by the server:-

The requested resource [/Doctor_Appointment_Application/Regis] is not available

I am using annotations to register my servlets and there is only a welcome file in my web.xml.

Web.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app version=&quot;4.0&quot;
	xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee 
   http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
	&lt;display-name&gt;User Login&lt;/display-name&gt;
	&lt;session-config&gt;
		&lt;session-timeout&gt;30&lt;/session-timeout&gt;
	&lt;/session-config&gt;
	&lt;welcome-file-list&gt;
		&lt;welcome-file&gt;/JSP/login.jsp&lt;/welcome-file&gt;
	&lt;/welcome-file-list&gt;
&lt;/web-app&gt;

My Register Servlet:-

package com.servlet;

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;

import com.bean.RegisBean;
import com.dao.RegisDao;

@WebServlet(&quot;/Regis&quot;)
public class Regis extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public Regis() {

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String fullname=request.getParameter(&quot;fullname&quot;);
		String email=request.getParameter(&quot;email&quot;);
		String username=request.getParameter(&quot;username&quot;);
		String password=request.getParameter(&quot;password&quot;);
		long mobile= Long.parseLong(request.getParameter(&quot;mobile&quot;));
		String address=request.getParameter(&quot;address&quot;);
		String identity=request.getParameter(&quot;radio&quot;);
		String specialisation=request.getParameter(&quot;special&quot;);
		String degree=request.getParameter(&quot;degree&quot;);
		String exp=request.getParameter(&quot;exp&quot;);
		String fees=request.getParameter(&quot;fees&quot;);
		
		RegisBean regisBean=new RegisBean();
		regisBean.setFullname(fullname);
		regisBean.setEmail(email);
		regisBean.setUsername(username);
		regisBean.setPassword(password);
		regisBean.setMobile(mobile);
		regisBean.setAddress(address);
		regisBean.setIdentity(identity);

		if(identity.equals(&quot;Doctor&quot;)) {
			regisBean.setSpecialiasation(specialisation);
			regisBean.setDegree(degree);
			regisBean.setExperience(exp);
			regisBean.setFees(fees);
		}
		
		RegisDao dao=new RegisDao();
		String userRegistered=dao.registerUser(regisBean);
		if(userRegistered.equals(&quot;SUCCESS&quot;))
			request.getRequestDispatcher(&quot;/JSP/conf.jsp&quot;).forward(request, response);
		else {
			request.setAttribute(&quot;errMessage&quot;, userRegistered);
			request.getRequestDispatcher(&quot;/JSP/register.jsp&quot;).forward(request, response);
		}
	}
}

EDIT 1:

Register.jsp:-

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;ISO-8859-1&quot;&gt;
&lt;title&gt;Registration Page&lt;/title&gt;
&lt;style type=&quot;text/css&quot;&gt;
&lt;%-- &lt;%@include file=&quot;/CSS/register.css&quot;%&gt; --%&gt;
&lt;%@include file=&quot;/CSS/regis.css&quot;%&gt;
&lt;/style&gt;
&lt;script src=&#39;https://kit.fontawesome.com/a076d05399.js&#39;&gt;&lt;/script&gt;
&lt;script&gt;
function validate() {
		var password = document.form.password.value;
		var confpassword = document.form.confpassword.value;

/* 		if (password.length &lt; 6) {
			alert(&quot;Password must be at least 6 characters long.&quot;);
			return false;
		} else */ if (password != confpassword) {
			alert(&quot;Confirm Password should match with the Password&quot;);
			return false;
		}
	}

 &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;form name=&quot;form&quot; action=&quot;&lt;%=request.getContextPath() %&gt;/Regis&quot; method=&quot;post&quot; onsubmit=&quot;return validate()&quot;&gt;
		Full Name: &lt;input type=&quot;text&quot; name=&quot;fullname&quot; required&gt;
		&lt;br&gt;
		&lt;br&gt; 
		Email: &lt;input type=&quot;text&quot; name=&quot;email&quot; required&gt;
		&lt;br&gt;
		&lt;br&gt; 
		Username: &lt;input type=&quot;text&quot; name=&quot;username&quot; required&gt; 
		&lt;br&gt;
		&lt;br&gt; 
		Password: &lt;input type=&quot;password&quot; name=&quot;password&quot; required&gt; 
		&lt;br&gt;
		&lt;br&gt; 
		Confirm Password: &lt;input type=&quot;password&quot; name=&quot;confpassword&quot; required&gt;
		&lt;br&gt;
		&lt;br&gt;
		Mobile Number: &lt;input type=&quot;number&quot; name=&quot;mobile&quot; required&gt;
		&lt;br&gt;
		&lt;br&gt;
		Address: &lt;input type=&quot;text&quot; name=&quot;address&quot; required&gt;
		&lt;br&gt;
		&lt;br&gt;	
		Identity: &amp;nbsp; Doctor &lt;input type=&quot;radio&quot; name=&quot;radio&quot; value=&quot;Doctor&quot; required&gt;
		&lt;div class=&quot;reveal&quot;&gt;
			Specialisation: &lt;select name=&quot;special&quot; class=&quot;require-if-active&quot;&gt;
				&lt;option selected disabled&gt;Choose...&lt;/option&gt; 
				&lt;option value=&quot;Allergist&quot;&gt;Allergist&lt;/option&gt;
				&lt;option value=&quot;Anesthesiologist&quot;&gt;Anesthesiologist&lt;/option&gt;
				&lt;option value=&quot;Cardiologist&quot;&gt;Cardiologist&lt;/option&gt;
				&lt;option value=&quot;Dermatologist&quot;&gt;Dermatologist&lt;/option&gt;
				&lt;option value=&quot;Endocrinologist&quot;&gt;Endocrinologist&lt;/option&gt;
				&lt;option value=&quot;Gastroenterologist&quot;&gt;Gastroenterologist&lt;/option&gt;
				&lt;option value=&quot;Hematologist&quot;&gt;Hematologist&lt;/option&gt;
				&lt;option value=&quot;Immunologist&quot;&gt;Immunologist&lt;/option&gt;
				&lt;option value=&quot;Internist&quot;&gt;Internist&lt;/option&gt;
				&lt;option value=&quot;Neurologist&quot;&gt;Neurologist&lt;/option&gt;
				&lt;option value=&quot;Pulmonologist&quot;&gt;Pulmonologist&lt;/option&gt;
				&lt;option value=&quot;Oncologist&quot;&gt;Oncologist&lt;/option&gt;
			&lt;/select&gt;
			&lt;br&gt;
			&lt;br&gt;
			Degree: &lt;select name=&quot;degree&quot; class=&quot;require-if-active&quot;&gt;
				&lt;option selected disabled&gt;Choose...&lt;/option&gt; 
				&lt;option value=&quot;MBBS&quot;&gt;MBBS&lt;/option&gt;
				&lt;option value=&quot;BDS&quot;&gt;BDS&lt;/option&gt;
				&lt;option value=&quot;BAMS&quot;&gt;BAMS&lt;/option&gt;
				&lt;option value=&quot;BUMS&quot;&gt;BUMS&lt;/option&gt;
				&lt;option value=&quot;BHMS&quot;&gt;BHMS&lt;/option&gt;
				&lt;option value=&quot;BYNS&quot;&gt;BYNS&lt;/option&gt;
				&lt;option value=&quot;B.V.Sc &amp; AH&quot;&gt;B.V.Sc &amp; AH&lt;/option&gt;
			&lt;/select&gt;
			&lt;br&gt;
			&lt;br&gt;
			Experience: &lt;input type=&quot;number&quot; name=&quot;exp&quot; class=&quot;require-if-active&quot;&gt;
			&lt;br&gt;
			&lt;br&gt;
			Fees: &lt;input type=&quot;number&quot; name=&quot;fees&quot; class=&quot;require-if-active&quot;&gt;
		&lt;/div&gt;
		Patient &lt;input type=&quot;radio&quot; name=&quot;radio&quot; value=&quot;Patient&quot; required&gt;
		&lt;span style=&quot;color: red&quot;&gt;&lt;%=(request.getAttribute(&quot;errMessage&quot;) == null) ? &quot;&quot; : request.getAttribute(&quot;errMessage&quot;)%&gt;&lt;/span&gt;
		&lt;br&gt;
		&lt;br&gt; &lt;input type=&quot;submit&quot; value=&quot;Register&quot;&gt;
	&lt;/form&gt;	
&lt;/body&gt;
&lt;/html&gt;

EDIT 2:

I just tried to do an experiment, what I did is I created another jsp file and a servlet to check if the problem is in my jsp and servlet files or not. So the same thing happened, the jsp file ran successfully but the server wasn't abl to find the servlet after submitting the form method.

Any kind of help is appreciated.

答案1

得分: 1

你好,无论是谁看到这个消息,
我通过从构建路径中移除mysqljdbc.auth.dll文件来解决了这个查询。这解决了我的问题,现在它正常工作。如果你遇到mysqljdbc.auth.dll文件的问题,你只需要将它复制到JDK 8的bin文件夹中,它就会正常工作。

英文:

Hello to whosoever is seeing this,
I solved this query by removing the mysqljdbc.auth.dll from my build path. This solved my problem and it is working fine now. If you are having a problem with the mysqljdbc.auth.dll file, you just have to copy it in the JDK 8 bin folder and it will work fine.

答案2

得分: 0

更新(基于问题中发布的更新):

&lt;form name=&quot;form&quot; action=&quot;&lt;%=request.getContextPath() %&gt;/Regis&quot; method=&quot;post&quot; onsubmit=&quot;return validate()&quot;&gt;

替换为

&lt;form name=&quot;form&quot; action=&quot;Regis&quot; method=&quot;post&quot; onsubmit=&quot;return validate()&quot;&gt;

因为在 JSP 中,路径已经相对于上下文路径。

原始回答:

你收到这个错误的原因是,默认情况下,服务器将请求视为 GET 请求,而你的 servlet 中没有提供 doGet 的实现。如果你从 JSP/HTML 调用此 servlet,请确保指定 method=&quot;POST&quot;。如果你试图直接调用 servlet,你可以将 doPost 重命名为 doGet,或者提供 doGet 的实现,例如:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
}
英文:

Update (based on the update posted in the question):

Replace

&lt;form name=&quot;form&quot; action=&quot;&lt;%=request.getContextPath() %&gt;/Regis&quot; method=&quot;post&quot; onsubmit=&quot;return validate()&quot;&gt;

with

&lt;form name=&quot;form&quot; action=&quot;Regis&quot; method=&quot;post&quot; onsubmit=&quot;return validate()&quot;&gt;

as the paths in a JSP are already relative to the context path.

Original answer:

The reason why you are getting this error is that by default, the server gets the request as GET whereas you have not provided any implementation of doGet in your servlet. If you are calling this servlet from a JSP/HTML, make sure you mention method=&quot;POST&quot;. If you are trying to call the servlet directly, you can either rename doPost to doGet or provide an implementation of doGet e.g.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doPost(request, response);
}

huangapple
  • 本文由 发表于 2020年10月11日 01:41:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64296439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定