JavaWeb的阶段学习内容(重点内容)

一.必备知识。c1w

1.静态web
*.htm, *.html这些都是网员的后境、如果服务器上一直存在这些东四,我们就可以直接进行读取、需要网络。

静态web存在的缺点:

Web页面无法动态更新,所有用户看到都是同一个页面
轮播图,点击特效:伪动态
JavaScript[实际开发中,它用的最多]
VBScript
它无法和数据库交互(数据无法持久化,用户无法交互。

2.动态web
页面会动态展示,产生个体变化,更加有个性。

缺点:

加入服务器的动态web资源出现了错误,我们需要重新编写我们的后台程序,重新发布;
停机维护优点:
Web页面可以动态更新,所有用户看到都不是同一个页面
它可以与数据库交互。

二.安装配置tomcat

1.下载链接:http://tomcat.apache.org/

安装的时候要注意自己的环境,windows的安装windows,Linux安装linux的,我自己下载的是:apache-tomcat-10.0.27

2.tomcat的文件夹结构:

bin-启动,关闭的脚本文件

conf-配置文件夹

lib-依赖包

webapp:存放网站的

bin目录下的:

    shutdown.bat:关闭服务器

    startup.bat:启动服务器

conf下的server.xml:核心配置文件

 下面可以修改:

  • tomcat的默认端口号为:8080
  • mysql:3306
  • http:80
  • https:443
  • 配置端口号
  • <Connector port="8081" protocol="HTTP/1.1"
          connectionTimeout="20000"
          redirectPort="8443" />
    

    默认的主机名为:localhost->127.0.0.1

  • 默认网站应用存放的位置为:webapps配置主机的名称:

  •   <Host name="www.qinjiang.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
    

3.访问测试:http://localhost:8080/

4.启动失败的原因:

     Java环境变量出了问题

     闪退问题:需要配置兼容性

      乱码问题:配置文件中设置conf/logging.properties 中的java.util.logging.ConsoleHandler.encoding = GBK

5.自己发布web网站

将自己的网站,放到tomcat中指定的wen应用的web应用的文件夹(webapp)下,就可以访问了

网站的结构:

--webapps :Tomcat服务器的web目录
	-ROOT
	-kuangstudy :网站的目录名
		- WEB-INF
			-classes : java程序
			-lib:web应用所依赖的jar包
			-web.xml :网站配置文件
		- index.html 默认的首页
		- static 
            -css
            	-style.css
            -js
            -img
         -.....

三.Http

1.超文本传输协议是一个简单的请求-响应协议,通常运行在TCP之上。

文本:html,字符串

超文本:图片,音乐,视频,定位,地图

端口:80

Https:安全的

2.http的发展:

http1.0:客户端可以与web服务器连接后,只能获得一个web资源,断开连接

http2.0:客户端可以与web服务器连接后,可以获得多个web资源。

3.Http请求

客户端-发请求(Request)-服务器

百度的:

Request URL:https://www.baidu.com/   请求地址
Request Method:GET    get方法/post方法
Status Code:200 OK    状态码:200
Remote(远程) Address:14.215.177.39:443

Accept:text/html  
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.9    语言
Cache-Control:max-age=0
Connection:keep-alive

请求行:

请求行中的请求方式:GET
请求方式:Get,Post,HEAD,DELETE,PUT,TRACT.…
get:请求能够携带的参数比较少,大小有限制,会在浏览器的URL地址栏显示数据内容,不安全,但高效
post:请求能够携带的参数没有限制,大小没有限制,不会在浏览器的URL地址栏显示数据内容,安全,但不高效。

消息头:

Accept:告诉浏览器,它所支持的数据类型
Accept-Encoding:支持哪种编码格式  GBK   UTF-8   GB2312  ISO8859-1
Accept-Language:告诉浏览器,它的语言环境
Cache-Control:缓存控制
Connection:告诉浏览器,请求完成是断开还是保持连接
HOST:主机..../.

4.Http响应:

服务器---响应---客户端

百度:

Cache-Control:private    缓存控制
Connection:Keep-Alive    连接
Content-Encoding:gzip    编码
Content-Type:text/html   类型  

响应体:

Accept:告诉浏览器,它所支持的数据类型
Accept-Encoding:支持哪种编码格式  GBK   UTF-8   GB2312  ISO8859-1
Accept-Language:告诉浏览器,它的语言环境
Cache-Control:缓存控制
Connection:告诉浏览器,请求完成是断开还是保持连接
HOST:主机..../.
Refresh:告诉客户端,多久刷新一次;
Location:让网页重新定位;

响应码状态:

200:请求响应成功。

3XX:请求重定向

4XX:找不到资源,资源不存在

5XX:服务器代码错误

502:网关错误

四.Maven

1.下载安装配置:

官网:Maven – Welcome to Apache Maven

解压安装到自己的电脑,然后先配置环境变量:

MAVEN_HOME:自己的maven目录

path:中配置%MAVEN_HOME%bin

之后打开cmd命令行:mvn -version显示了maven的版本和Java信息就是ok了。

修改maven的配置目录:打开maven目录下的conf,里面的setting.xml进行配置修改:

记得留意JDK版本和文件编码,还有配置阿里的仓库,特别是jdk的版本,我的是1.8的

本人的setting.xml文件如下:

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

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
<-- 配置本地仓库:D:\java\maven\repository-->
	<localRepository>D:\java\maven\repository</localRepository>
  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
  <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
<-- 配置阿里云仓库,国内的东西,快不少-->
	 <mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>

  </mirrors>

  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
<--配置JDK的版本:本人使用的是1.8,对于目前的开发来说够用了,而且这里很重要,后面Idea利用本地的maven来创建项目的时候就要利用里面的Java版本,记得项目或者模块的jdk要与maven里配置的一致,不然后面建了项目也运行不起来,爆错-->
	<profile>     
		<id>JDK-1.8</id>       
		<activation>       
			<activeByDefault>true</activeByDefault>       
			<jdk>1.8</jdk>       
		</activation>       
		<properties>       
			<maven.compiler.source>1.8</maven.compiler.source>       
			<maven.compiler.target>1.8</maven.compiler.target>       
			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>       
		</properties>       
	</profile>
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>

五.Servlet

1.Servlet简介

  • Servlet就是sun公司开发动态web的一门技术
  • Sun在这些APi中提供一个接口叫做:Servlet,如果你想开发一个Servlet程序,只需要完成两个小步骤:
    • 编写一个类,实现Serlet接口
    • 把开发好java类部署到web服务器中。

 把实现了Servlet接口的程序称为Servlet

Servlet接口Sun公司提供了两个默认的实现类:HttpServlet,GenericServled

2.创建项目:

Idea里面创建一个空项目,叫JavaWebStudy,

然后里面创建一个新的模块叫做servlet-01,就普通的Jakarta EE8项目,记得勾选Web应用程序还有tomcat服务器,工件ID和组ID自己取就欧克了,不要选后面的什么版本,一路点确认。

接着在项目上面的小锤子添加配置,构建里面配置tomcat本地服务器,配置端口号和默认打开的浏览器,以及部署更新和web程序名称,,配置上下文就是你后面访问端口的后缀,直接/,表示不用什么后缀,直接浏览器地址栏输入8080进行访问,配置了servlet-01就是8080/servlet-01访问

 编辑tomcat的部署配置:选择自己之前安装的tomcat的目录

4.关于idea的配置我的文件编码是这样的:

 idea的maven的配置

 项目结构里的Java language level情况:

 这些细节配置要注意,不然后面麻烦得很

5.接下来开始开发:

在src/main/java/com/liushan/servlet01里面建个自己的Servlet:

package com.liushan.servlet01;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
        httpServletResponse.setContentType("text/html;charset=utf-8");//解决中文乱码问题,一般浏览器的编码为GBK,而我开发环境是utf-8
        httpServletResponse.setCharacterEncoding("utf-8");//解决中文乱码问题
        httpServletResponse.getWriter().println("你好");//使用HttpResponse的getWriter().println()输出你好到页面
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

在src/main/webapp/WEB-INF/web.xml里面配置相关的mapper:

    <!--注册Servlet-->
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.liushan.servlet01.HelloServlet2</servlet-class>
    </servlet>
    <!--Servlet的请求路径-->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

然后启动tomcat程序,访问localhost:8080/servlet-01/helloj​​​​​​

就可以看到页面有”你好“二字

一个servlet可以映射多个路径,一个Servlet可以指定映射路径,可以自定义后缀实现请求映射 注意点,*前面不能加项目映射的路径,指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求

此时我的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>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.liushan.servlet01.HelloServlet2</servlet-class>
    </servlet>
    <!--Servlet的请求路径-->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
<!--一个Servlet可以映射多个路径-->

<!--    <servlet-mapping>-->
<!--        <servlet-name>hello</servlet-name>-->
<!--        <url-pattern>/hello2</url-pattern>-->
<!--    </servlet-mapping>-->
<!--    <servlet-mapping>-->
<!--        <servlet-name>hello</servlet-name>-->
<!--        <url-pattern>/hello3</url-pattern>-->
<!--    </servlet-mapping>-->
<!--    <servlet-mapping>-->
<!--        <servlet-name>hello</servlet-name>-->
<!--        <url-pattern>/hello4</url-pattern>-->
<!--    </servlet-mapping>-->
<!--    <servlet-mapping>-->
<!--        <servlet-name>hello</servlet-name>-->
<!--        <url-pattern>/hello5</url-pattern>-->
<!--    </servlet-mapping>-->
<!--    一个Servlet可以指定映射路径-->
<!--    <servlet-mapping>-->
<!--        <servlet-name>hello</servlet-name>-->
<!--        <url-pattern>/hello/*</url-pattern>-->
<!--    </servlet-mapping>-->
<!--   默认请求路径-->
<!--    <servlet-mapping>-->
<!--        <servlet-name>hello</servlet-name>-->
<!--        <url-pattern>/*</url-pattern>-->
<!--    </servlet-mapping>-->

    <!--可以自定义后缀实现请求映射
        注意点,*前面不能加项目映射的路径
        -->
<!--    <servlet-mapping>-->
<!--        <servlet-name>hello</servlet-name>-->
<!--        <url-pattern>*.liushan</url-pattern>-->
<!--    </servlet-mapping>-->
<!--指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求-->
    <!--404-->
<!--    <servlet>-->
<!--        <servlet-name>error</servlet-name>-->
<!--        <servlet-class>com.liushan.servlet01.Error</servlet-class>-->
<!--    </servlet>-->
<!--    <servlet-mapping>-->
<!--        <servlet-name>error</servlet-name>-->
<!--        <url-pattern>/*</url-pattern>-->
<!--    </servlet-mapping>-->


</web-app>

六.ServletContext

web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用

面试题:

1.请你谈谈网站是如何进行访问的!

输入一个域名;回车
检查本机的C:\Windows\System32\drivers\etc\hosts配置文件下有没有这个域名映射;
有:直接返回对应的ip地址,这个地址中,有我们需要访问的web程序,可以直接访问

127.0.0.1 www.qinjiang.com
没有:去DNS服务器找,找到的话就返回,找不到就返回找不到;

.可以配置一下环境变量(可选性)
2.HTTP协议:面试

Maven:构建工具

  • Maven安装包

Servlet入门

  • HelloWorld!
  • Servlet配置 ·原理
  • 常见面试题:
  • 当你的浏览器中地址栏输入地址并回车的一瞬间到页面能够展示回来,经历了什么?
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值