Eslicpe安装
下载地址,https://www.eclipse.org/downloads/
tomcat服务器安装
下载地址,https://tomcat.apache.org/download-80.cgi
Dynamic web project
如果你的软件里没有它,请向下看,反之,跳过。
- 选择help->Install New Software,并进入
- 点击Next,完成后重启即可。
- 选择Window->Perspective->Customize Perspective,并进入勾选Dynamic web project
建立Web工程
- 选择建立Dynamic web project
- 进入页面,选择NewRuntime
- 选择Apache Tomcat v8.5,选择NewRuntime
- 选择Apache Tomcat 安装目录,并填写Name
- 点击finish,finish即可。
配置文件
home.java
package web;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 home
*/
@WebServlet("/home")
public class home extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public home() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
request.setAttribute("message", "御主人様、お早う御座います" + " 時間:" + df.format(new Date()));
request.getRequestDispatcher("index.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
index.jsp
在如图目录建立index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta name="content-type" content="text/html; charset=UTF-8">
<title>first page</title>
</head>
<body>
<div align="center">
${message }
</div>
</body>
</h
添加配置服务器
在原服务器上添加应用(右键选择Add and Remove)
运行应用
在服务器上右键Run,在浏览器输入http://127.0.0.1:8080/web/home,localhost:8080/web/home