javaweb tomcat的使用

B/S与C/S的区别

B/S browser server 安全性低对客户要求低

C/S client server 安全性高对客户要求高

动态网页:需要与后台服务器进行交互数据的网页
tomcat:

1.解压版:在开发时使用

2.安装版:在项目完成时使用

tomcat是使用java开发,所以要有jdk

设置环境变量:

名字:java-home

值:java的bin目录

使用:

在解压文件后打开bin>startup.bat文件,打开后如下

浏览器输入 localhost:8080 or 127.0.0.1:8080会弹出tomcat欢迎界面如

浏览器的端口号默认是80

xml:主要做配置文件(具有层次关系的配置文件)

propertier:主要保存key=value的配置文件

在tomcat上传文件的方式

1.将文件放入webapps中,在浏览器的导航条输入localhost:8080/文件名(精确到要打开的文件)

2.在配置文件(conf文件夹)打开sever.xml,在Host标签中添加一个context标签(<Context path="/name" docBase="路径"><Context>)在浏览器的导航条输入localhost:8080/name

3.在conf>Catalina>locahost创建一个xml文件(name.xml)在xml文件中添加一个context标签(<Context docBase="路径"><Context>)在浏览器的导航条输入localhost:8080/xml文件名/文件名(精确到要打开的文件)

在使用2,3时修改完xml文件时要重启tomcat

修改端口号

在sever.xml中的Connector标签的port值(<Connector port="8888">)//在修改端口号时先查看端口是否被占用

查看所有端口占用:

netstat -ano

查找特定端口(如8080):

netstat -ano | findstr 8080

servlet 服务器小程序

在webapps创建一个文件夹myweb为根目录

放入js,css,html文件或文件夹

放入WEB-INF文件夹

在WEB-INF文件夹放入classes,lib文件夹与web.xml配置文件

结构:

myweb文件夹-->

js文件夹

css文件夹

index.html

WEB-INF文件夹-->

lib文件夹(存放jar)

web.xml

classes文件夹-->

JavaTime.java

JavaTime.class

在普通的java类中继承了HttpServlet,普通的java类就变成了Servlet,具有处理网页请求的能力如

public class JavaTime extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//重写doGet

response.setContentType("text/html");

PrintWriter out = response.getWriter();向浏览器打印

out.println("hello!!");//内容

out.flush();

out.close();//关流

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//重写doPost

doGet(request, response);

}

}

在web.xml文件修改

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

<servlet>

<servlet-name>JavaTime</servlet-name>//JavaTime为类名

<servlet-class>JavaTime</servlet-class>//JavaTime为全限定名,如果有放入其他包就为包名.类名

</servlet>

<servlet-mapping>

<servlet-name>JavaTime</servlet-name>//JavaTime为类名

<url-pattern>/lty</url-pattern>//虚拟目录

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

index为网页名,与创建结构时的名字一致

相关推荐
头发还在的女程序员4 小时前
医院陪诊管理系统怎么选择?——2026 年选型避坑与架构参考
java·开发语言·陪诊系统·陪诊app·医院陪诊陪护
CodeStats5 小时前
【Spring事务】Spring事务注解 @Transactional 完整体系:从 MySQL 隔离级别到 MyBatis 原理详解
java·spring·mybatis·事务·transactional
我命由我123455 小时前
Android 开发问题:为 PDFView 设置一个带有黑色边框的背景 drawable,但边框没有生效
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
都叫我大帅哥7 小时前
从Python到Java:为什么企业级Agent最终会选择Java?
java·ai编程
wanderist.8 小时前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法
腻害兔9 小时前
【若依项目-产品经理视角】RuoYi-Vue-Pro 源码拆解:IM 即时通讯模块,一个被低估的「全功能聊天系统」
java·前端·vue.js·产品经理·ai编程
心念枕惊9 小时前
.NET CORE 授权进阶-角色、策略与动态权限实现
java·前端·.netcore
景同学11 小时前
把 AI 用到线上运维:可行、有效,前提是喂足信息——一次 Full GC 排障实录
java·人工智能·后端
C++、Java和Python的菜鸟11 小时前
第7章 后端Web实战(Tlias系统)
java
憧憬成为java架构高手的小白12 小时前
黑马八股--spring框架学习
java·学习·spring