您好我是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;
}
当我尝试进入SUBMIT按钮时,它显示错误
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 :(得分:0)
您是否可能使用demoServlet
而不是POST
以某种方式访问GET
servlet?这将解释您的错误消息。请检查您的JavaScript控制台,确保它使用GET
。
另外,请将您的doPost()
方法修改为以下内容:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
如果这解决了错误,那么我们就解决了问题。
答案 1 :(得分:0)
这对我有用!我只从onsubmit事件属性中删除了return关键字。我还修改了一些代码,以便将用户名正确地分配给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;
}
答案 2 :(得分:0)
您的servlet代码看起来很好。您需要进行的唯一更改是在Validate.js文件中。 将用户名验证为空时,您使用了“=”:
if(username=""){ // your code }
相反,你使用“==”,它工作正常。
if(username==""){ // your code }