嗨我是新的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...?
你缺少一個右引號中結束:** \'/ myWebprj/**的success.jsp是 –
沒有在js函數指派返回值虛假什麼特別的原因。你是否厭倦了使用空弦。只是猜測 –
請向我們展示調用servlet的UI代碼。我確信有一些非常小的錯誤導致你的問題。 –