2015-09-03 67 views
-1

我有一個.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> 

我想打電話給一個servletLogin按鈕的點擊。該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被放置在同一個項目中:enter image description here

任何幫助非常感謝!

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> 
+3

你確定你已經在你的web.xml中聲明瞭你的servlet和servlet映射嗎? –

+0

是的。做到了。 – TheLuminor

+0

您是否嘗試過重定向而不是使用打印作者? –

回答

4

看起來你的Servlet在中沒有正確映射或通過annotation

首先檢查你的web.xml正確映射: 讓我們假設你的LoginServlet是包下aa.bb.cc。如果是這樣的話:

<servlet> 
    <servlet-name>LoginServlet</servlet-name> 
    <servlet-class> 
     aa.bb.cc.LoginServlet 
    </servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>LoginServlet</servlet-name> 
    <url-pattern>/LoginServlet</url-pattern> 
</servlet-mapping> 

我看不到您的JSP文件的任何問題。所以移動到你的servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
    System.out.println("inside servlet 2"); 
    String XPID = request.getParameter("exampleXPID"); 
    String password = request.getParameter("exampleInputPassword1"); 

    PrintWriter out = response.getWriter(); 
    out.println("<html><body><script>alert("+XPID+")</script></body></html>"); 
} 

修訂

你的web.xml文件是不正確的..它應該是:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
<servlet> 
    <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> 

更新2

另一個問題是您已經導入了錯誤的Response類:

import javax.ws.rs.core.Response; 

它應該是:

import javax.servlet.http.HttpServletResponse; 

希望它應該管用。

+1

我試過你說的。我得到錯誤500.編輯問題以供參考。請看看它!謝謝! – TheLuminor

+0

你的名字是什麼?它清楚地表明您的servlet名稱沒有找到.. –

+0

在這裏發佈您的Servlet類.. –

2

可能是試試這個...

包括名稱爲您的按鈕

<button type="submit" name="button1" class="btn btn-primary">Login</button> 

在Servlet中,

If(request.getParameter("button1")!=null) 
{ 
    String XPID = request.getParameter("exampleXPID"); 
    String password = request.getParameter("exampleInputPassword1"); 
    . 
    . 

希望這有助於你...

+0

,但錯誤說它不能訪問'LoginServlet'本身!我很困惑你的解決方案將如何幫助。你能詳細說明一下嗎? @JavaLearner – TheLuminor

1

你忘了你的servlet來初始化打印作家:

PrintWriter out = response.getWriter(); 

UPDATE

// Set the MIME type for the response message 
    response.setContentType("text/html"); 

    // Write to network 
    PrintWriter out = response.getWriter(); 
+0

我以前試過做過啊。但是當我這樣做時,我得到一個錯誤,說'響應無法解決'。有任何想法嗎? – TheLuminor

+0

將此代碼放在打印作者之前, 'response.setContentType(「text/html」);' 這就是指定什麼是響應消息的MIME類型。 我也更新了我上面的答案。 – Makudex

+0

試過這個。但它仍然說'錯誤500'。同樣的日誌在我的問題。 :/ – TheLuminor