gpt4 book ai didi

java - 如何在 Eclipse 中运行 servlet?

转载 作者:行者123 更新时间:2023-11-28 23:38:50 29 4
gpt4 key购买 nike

安装细节:

先安装jdk然后添加环境变量

变量名=“路径”

变量值="C:\Program Files\Java\jdk1.7.0_45\bin"(java目录)

然后提取eclipse和tomcat

解压后,我编辑 apache-tomcat-7.0.23\bin 中的 catalina.bat 并添加:"设置 JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45"

然后我通过创建新的动态 Web 项目制作了我的第一个 Servlet

<!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=UTF-8">
<title>Test Form</title>
</head>
<body>

<h1>JAVA TEST</h1>

<form action ="work.html" method ="get">

<input type="submit" value="login">
</form>



</body>
</html>

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class work
*/
@WebServlet("/work.html")
public class work extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public work() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.print("<html>");
out.print("<head>");
out.print("<title>Login Authentication Result</title>");
out.print("<h2>Welcome to servlet</h2>");
out.print("<p> powered by" + getServletContext().getServerInfo()+ "</p>");
out.print("</body>");
out.print("</hmtl>");
out.close();
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Test3</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

enter image description here

我是 servlet 的新手。不知道为什么点击index.html中的按钮总是报错

enter image description here

我想我也有同样的problem

最佳答案

只需在eclipse中配置Tomcat即可!

关于java - 如何在 Eclipse 中运行 servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21948873/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com