【发布时间】:2018-02-02 03:57:35
【问题描述】:
我有足够的知识使用 Java SE/JavaFX(桌面)进行开发,但这是我第一次使用 Java EE(WEB)。我想构建一个基本应用程序,仅在 HTML/JSP 中获取登录名和密码,调用 Servlet(在 Java 中)并向 HTML/JSP 返回一条消息。这次我想用“艰难的方式”做到这一点,没有任何 IDE。所以,我已经安装了 Tomcat 7.0,并且拥有了这些模块:
Java(本例仅从 HTML/JSP 接收)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class loginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String userName = request.getParameter("userName");
String password = request.getParameter("password");
out.println("<html>");
out.println("<body>");
out.println("Hello " + " " + userName + "welcome to my blog");
out.println("Your password is : " + " " + password + "<br>");
out.println("</body></html>");
}
}
HTML/JSP
<!DOCTYPE html>
<html lang ="pt-br">
<head>
<title> loginServlet </title>
<meta http-equiv = ”Content-Type” content=”text/html; charset=UTF-8”>
<link rel="stylesheet" type="text/css"
href="c:/java/html/css/estilo.css"/>
</head>
<body>
<h2> Login Page </h2>
<p>Please enter your username and password</p>
<form method="GET" action="loginServlet">
<p> Username <input type="text" name="userName" size="50"> </p>
<p> Password <input type="text" name="password" size="20"> </p>
<p> <input type="submit" value="Submit" name="B1"> </p>
</form>
</body>
</html>
WEB.XML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>loginServlet</display-name>
<description>
This is a simple web application with a source code organization
based on the recommendations of the Application Developer's Guide.
</description>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>java.loginServlet.WebContaint.WEB-
INF.classes.loginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>
路径和文件夹
loginServlet.class在C://java/loginServlet/WebContaint/WEB-INF/classes/中loginServlet.jsp在C://java/loginServlet/WebContaint/中web.xml在C://java/loginServlet/WebContaint/WEB-INF/中loginServlet.war在C:\Program Files\Apache Software Foundation\ Tomcat 7.0\webapps中
当我尝试使用本地主机 (http://localhost:8080/loginServlet) 调用 Chrome 时,我收到 404 错误。
【问题讨论】:
-
Javascript 问题在哪里?
-
我认为我的问题可能是在我的 HTML 和我的 Servlet Java 之间的某个地方,或者可能是 Tomcat。我不知道。
-
查看此示例以获取带有 Tomcat 的 web.xml 文件,看看您可能缺少什么:wiki.metawerx.net/wiki/Web.xml 对我来说,您似乎缺少
部分,并且我认为你的包名包含太多信息,删除“WEB-INF”的东西
标签: java html web-services servlets