2015-05-05 50 views
5

嗨我是新的Java編碼,我試圖在eclipse中創建動態網頁。這是我的demoServlet.java代碼Servlet響應沒有顯示在網頁上

import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.RequestDispatcher; 
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("/demoServlet") 
public class demoServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 
    public demoServlet() { 
     super(); 
    } 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
      response.setContentType("text/html"); 
      PrintWriter out= response.getWriter(); 
      out.println("<!DOCTYPE html PUBLIC \''>"); 
      out.println("<html>"); 
      out.println("<head>"); 
      out.println("<script language=\'JavaScript\'>"); 
      RequestDispatcher rd= request.getRequestDispatcher("/WEB-INF/javascript/Validate.js"); 
      rd.include(request, response); 
      out.println("</script>"); 
      out.println("</head>"); 
      out.println("<body>"); 
      out.println("<form action=\'/myWebprj/Success.jsp onSubmit=\'return Validateusername(this.username.value)\'>"); 
      out.println("UserName: <input type=\'text\' name=\'username\'>"); 
      out.println("<input type=\'submit\' value=\'Submit\'>"); 
      out.println("</form>"); 
      out.println("</body>"); 
      out.println("</html>"); 
    } 
    protected void doPost(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
    } 
} 

這是我的JSP頁面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<!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=ISO-8859-1"> 
     <title>My First jsp page in servlets</title> 
    </head> 
    <body> 
     Its Successfully completed 
    </body> 
</html> 

,這是我的JavaScript頁面

function Validateusername(username) 
{ 
    var returnValue=true; 
    if(username=""){ 
     alert("Enter username is empty"); 
     returnValue=false; 
    } 
    return returnValue; 
} 

當我試圖進入提交按鈕它向我展示錯誤

HTTP Status 404 - /myWebprj/Success.jsp%20onSubmit= 

-------------------------------------------------------------------------------- 

type Status report 

message /myWebprj/Success.jsp%20onSubmit= 

description The requested resource is not available. 

Can any one help me out . whats is the error in the code...? 
+0

你缺少一個右引號中結束:** \'/ myWebprj/**的success.jsp是 –

+0

沒有在js函數指派返回值虛假什麼特別的原因。你是否厭倦了使用空弦。只是猜測 –

+0

請向我們展示調用servlet的UI代碼。我確信有一些非常小的錯誤導致你的問題。 –

回答

0

Mig有可能你以某種方式訪問​​你的demoServlet servlet使用POST而不是GET?這將解釋你的錯誤信息。請檢查您的JavaScript控制檯以確定它使用的是GET

另外,請修改doPost()方法如下:

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

如果可以解決問題,那麼我們就解決問題了。

0

這適用於我!我只從onsubmit事件屬性中刪除了返回關鍵字。我也修改了一些代碼,以正確地將用戶名分配給javascript函數。

package test; 
 

 
import java.io.IOException; 
 
import java.io.PrintWriter; 
 
import javax.servlet.RequestDispatcher; 
 
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("/demoServlet") 
 
public class demoServlet extends HttpServlet { 
 
    private static final long serialVersionUID = 1L; 
 
    public demoServlet() { 
 
     super(); 
 
    } 
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 
      response.setContentType("text/html"); 
 
      PrintWriter out= response.getWriter(); 
 
       
 
      String username = request.getParameter("username"); 
 

 
      out.println("<!DOCTYPE html PUBLIC \''>"); 
 
      out.println("<html>"); 
 
      out.println("<head>"); 
 
      out.println("<script language=\'JavaScript\'>"); 
 
      RequestDispatcher rd= request.getRequestDispatcher("/WEB-INF/javascript/Validate.js"); 
 
      rd.include(request, response); 
 
      out.println("</script>"); 
 
      out.println("</head>"); 
 
      out.println("<body>"); 
 
      /* 
 
      onSubmit should call only 'Validateusername()' 
 
      so the the 'return' keyword has been removed 
 
      */ 
 
      out.println("<form action=\'/Test/Success.jsp\' onSubmit=\'Validateusername(" + username + ")\'>"); 
 
      out.println("UserName: <input type=\'text\' name=\'username\'>"); 
 
      out.println("<input type=\'submit\' value=\'Submit\'>"); 
 
      out.println("</form>"); 
 
      out.println("</body>"); 
 
      out.println("</html>"); 
 
    } 
 
    protected void doPost(HttpServletRequest request, 
 
      HttpServletResponse response) throws ServletException, IOException { 
 
     // TODO Auto-generated method stub 
 
    } 
 
}

function Validateusername(username) 
 
{ 
 
    var returnValue=true; 
 
    if(username == undefined || username == null || username == ""){ 
 
     alert("Enter username is empty"); 
 
     returnValue=false; 
 
    } 
 
    return returnValue; 
 
}

0

你的servlet代碼看起來不錯。您需要做的唯一更改是在Validate.js文件中。 當校驗用戶名空你曾用「=」:

if(username=""){ // your code } 

而是使用「==」,它工作正常。

if(username==""){ // your code }