我有一個.jsp
網頁,其中有如下形式:jsp中不會與Servlet
<form name="myForm" method="post" action="LoginServlet">
<div class="form-group">
<label for="exampleXPID">userid</label>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" class="form-control" name="exampleXPID" id="exampleXPID" placeholder="XPID" required />
</div>
<span id="usernameError" style="color:red;"></span>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-star"></span></span>
<input type="password" class="form-control" name="exampleInputPassword1" id="exampleInputPassword1" placeholder="Password" required />
</div>
<span id="passwordError" style="color:red;"></span>
</div>
<hr/>
<button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-lock"></span>Login</button>
<a href="#" style="float:bottom; max-width: 100%;" data-toggle="popover" data-trigger="hover" data-content="Click here to reset your existing Password" data-placement="bottom"><span class="glyphicon glyphicon-question-sign"></span>Reset your Password</a>
<p><br></p>
</form>
我想打電話給一個servlet
上Login
按鈕的點擊。該servlet
名稱爲:LoginServlet
中,我有以下幾點:
import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.ws.rs.core.Response;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public LoginServlet() {
// 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
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("inside servlet 2");
// Set the MIME type for the response message
response.setContentType("text/html");
// Write to network
PrintWriter out = response.getWriter();
String XPID = request.getParameter("exampleXPID");
String password = request.getParameter("exampleInputPassword1");
out.println("<html><body><script>alert("+XPID+")</script></body></html>");
}
}
當我運行該項目,它進入第一頁,當我打的登錄按鈕,我得到錯誤404說源未找到。我的LoginServlet
被放置在同一個項目中:
任何幫助非常感謝!
HTTP狀態500 - 錯誤:
type Exception report
message Error instantiating servlet class com.javacodegeeks.example.messenger.service.myservlet
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error instantiating servlet class com.javacodegeeks.example.messenger.service.myservlet
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
root cause
java.lang.ClassNotFoundException: com.javacodegeeks.example.messenger.service.myservlet
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.
這裏是我的web.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<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_2_5.xsd" version="2.5">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.javacodegeeks.example.messenger</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.javacodegeeks.example.messenger.service.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
你確定你已經在你的web.xml中聲明瞭你的servlet和servlet映射嗎? –
是的。做到了。 – TheLuminor
您是否嘗試過重定向而不是使用打印作者? –