java代码要用web服务器运行起来,才能被浏览器访问的到。
html文件挡在项目根目录里面(与WEB-INF同级)
启动tomcat,访问http://localhost:8080/OA/test.html
不推荐
1.新建动态web项目,项目名称随便写
2.定义字节码文件存放的位置
3.内容目录与前面对应,勾选创建web.xml
4.把Html文件放入webapp目录里
5.把该项目webapp的路径放入Tomcat的server.xml里
启动Tomcat
访问http://localhost:8080/test3.html
tips:
动态的web项目可以写jsp java代码,静态项目只能写html
新建项目后未能显示:下三角标 - Select Working Set - edit - add
WEB-INF下的classes是放字节码文件的 eclipse默认不显示,只有不显示才是正常的(害怕你删字节码文件)
我们可以之间去工作空间看classes文件是否存在
上面这种方式比较麻烦,每次都要去tomcat的安装目录里去启动,我们可以在eclipse里安装和配置Tomcat插件来方便操作,方式如下
1.把以下插件文件放到Eclipse文件夹下的plugins文件夹里,然后重启
2.把插件关联到Tomcat
上述只能访问html,不能访问java,如果要访问java,必须要用servlet
如何导入并查看源码?
见视频
eclipse输入提示怎么打开?
打开Eclipse并点击顶部菜单栏上的“Window”菜单项。
在下拉菜单中,选择“Preferences”选项。
在打开的窗口中,导航到“Java” > “Editor” > “Content Assist”。
在右侧窗格中,找到“Auto Activation”部分,并勾选“Enable auto activation”复选框。
在“Auto Activation triggers for Java”字段中,输入字符(“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.”),用于触发代码提示。您可以输入多个字符,但请注意不要删除现有字符。
单击“OK”按钮保存更改。
HttpServletTest
public class HttpServletTest extends HttpServlet {
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
System.out.println("初始化");
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
super.service(req, resp);
System.out.println("服务");
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
System.out.println("销毁");
}
}
servlet建好后,需要用web.xml来访问
<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>TomcatAndservletdisplay-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>HttpServletservlet-name>
<servlet-class>com.itsource.servlet._02HttpServlet.HttpServletTestservlet-class>
servlet>
<servlet-mapping>
<servlet-name>HttpServletservlet-name>
<url-pattern>/xxurl-pattern>
servlet-mapping>
web-app>
访问:localhost/xx
1.servlet的作用就是让我们的浏览器能访问到java代码
2.servlet是规范,java不去实现,由各大服务器厂商去实现
3.所以tomcat里面就有对servlet的实现包 jar
public class LifeServlet extends HttpServlet{
public LifeServlet(){
System.out.println("出生");
}
@Override
public void init() throws ServletException {
System.out.println("取名,上户口");
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("工作");
}
@Override
public void destroy() {
System.out.println("立遗嘱,正常死亡");
}
}
web.xml
<servlet>
<servlet-name>LifeServletservlet-name>
<servlet-class>com.itsource.servlet._03LifeServlet.LifeServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>LifeServletservlet-name>
<url-pattern>/xxxurl-pattern>
servlet-mapping>
启动tomcat,浏览器多次访问http://localhost/xxx,正常停止tomcat,看控制台
信息: Server startup in 632 ms
出生
取名,上户口
工作
工作
工作
十一月 20, 2023 3:20:25 下午 org.apache.catalina.core.StandardServer await
信息: A valid shutdown command was received via the shutdown port. Stopping the Server instance.
十一月 20, 2023 3:20:25 下午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["http-nio-80"]
十一月 20, 2023 3:20:25 下午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["ajp-nio-8009"]
十一月 20, 2023 3:20:26 下午 org.apache.catalina.core.StandardService stopInternal
信息: Stopping service Catalina
立遗嘱
十一月 20, 2023 3:20:26 下午 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextDestroyed()
< load-on-startup >:启动tomcat时即执行了构造与初始化方法,后面访问只会执行service方法
放在< servlet-class >下面,值不为负数,数值越大优先级越高
<servlet>
<servlet-name>LifeServletservlet-name>
<servlet-class>com.itsource.servlet._03LifeServlet.LifeServletservlet-class>
<load-on-startup>0load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>LifeServletservlet-name>
<url-pattern>/xxxurl-pattern>
servlet-mapping>
可以拿到配置参数,了解即可,有时候可以用来解决硬编码的问题,比如设置字符编码集
web.xml
<servlet>
<servlet-name>LifeServletservlet-name>
<servlet-class>com.itsource.servlet._03LifeServlet.LifeServletservlet-class>
<init-param>
<param-name>encodeparam-name>
<param-value>UTF-8param-value>
init-param>
<load-on-startup>0load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>LifeServletservlet-name>
<url-pattern>/xxxurl-pattern>
servlet-mapping>
LifeServlet
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println(config.getServletName());
System.out.println(config.getInitParameter("encode"));
System.out.println("init...");;
}
可以用@WebServlet注解取代在web.xml里配置servlet,简化编写流程
@WebServlet("/web")
public class MethodServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get方法");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post方法");
}
}
webapp下新建User.html
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
<form action="/web" method="get">
账号:<input type="text" name="name"><br>
密码:<input type="password" name="password">
<input type="submit" value="get">
form>
<br>
<form action="/web" method="post">
账号:<input type="text" name="name"><br>
密码:<input type="password" name="password">
<input type="submit" value="post">
form>
body>
html>
浏览器访问测试:http://localhost/User.html,两个按钮会走到对应的方法
doGet方法和doPost方法可以合并为service方法,不管post或get请求,都会走service方法
@WebServlet("/web")
public class MethodServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("我是service...");
}
}
可以拿到请求里的各种信息
@WebServlet("/web")
public class MethodServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
String password = req.getParameter("password");
System.out.println("name: "+name);
System.out.println("password: "+password);
String accept = req.getHeader("Accept");
System.out.println("Accept: "+accept);
}
}
用来向前端返回信息
@WebServlet("/web")
public class MethodServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
String password = req.getParameter("password");
System.out.println("name: "+name);
System.out.println("password: "+password);
String accept = req.getHeader("Accept");
System.out.println("Accept: "+accept);
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
if(!"小王".equals(name)){
writer.print("账号不正确"
);
return;
}
if(!"123456".equals(password)){
writer.print("密码不正确"
);
return;
}
writer.print("登录成功"
);
}
}