H5W3
当前位置:H5W3 > 问答 > 正文

java Servlet响应在网页上不显示

嗨,我是Java编码的新手,我试图在Eclipse中创建动态网页。这是我的demoServlet.java代码

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...?

最佳答案

您是否有可能以某种方式使用demoServlet而不是POST访问GET servlet?这将解释您的错误信息。请检查您的JavaScript控制台以确保它正在使用GET

另外,请将您的doPost()方法修改为以下内容:

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

如果这样可以解决错误,那么我们就可以解决问题。

本文地址:H5W3 » java Servlet响应在网页上不显示

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址