jsp-servlet(1)环境搭建(Tomcat和myeclipse)和基本概念

1 Tomcat安装

下载并解压;

点击bin目录下的start.bat文件启动(这里可能会报错,initinternal failed ,检查8080端口是不是被占用了,然后重新启动);

访问localhost:8080即可转到下面的官方网站,因为I:\apache-tomcat-8.5.37\webapps\ROOT下面部署了该网站的内容。

Apache Tomcat/8.5.37

2 myeclipse安装

https://blog.csdn.net/qq_41928258/article/details/80055331

破解时出现问题:点击crack.bat一闪而过。

解决方法:1)jdk.11版本太高,安装jdk1.8   2)把破解的文件复制到MyEclipse的安装根目录下,再点击crack.bat

(这两步可能只需要做第二步,如果不行,就先做第一步。)

3 在myeclipse2017中启动MyEclipse Tomcat8.5(自带的)

在控制窗口点击运行按钮,在浏览器输入localhost:8080就可以打开apache tomcat网站。

另外:

Windows-preferences-server-runtime environment-add-选择Tomcat的安装目录

4 在myeclipse中新建web项目

1)new web project

2)new HTML页面

3)发布项目

4)启动服务,访问

注:可能出现的问题:

 /hello项目中index.jsp被我删掉了:

/hellotest项目中的index.jsp被保留着:

上述问题的原因不明。

------更新-----

 是因为在我的hello项目中,根目录下只有hello.html和index.jsp文件;在hello/WEB-INF/web.xml的welcome语句中,是这几句话,所以当不输入http://localhost:8080/hello/hello.html或者http://localhost:8080/hello/index.jsp,它默认值就找不到文件,所以。会报错

<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>

5 servlet概述

视频来源http://www.sxt.cn/jsp-servlet/servlet.html

1)servlet是什么

a)概念:是一个java类

b)作用:是一个服务器端的小程序,处理用户请求。

c)为什么要有Servlet:因为传统的Java是不能处理网络应用程序的,所以引入了新的API,该Servlet类能响应网络请求。

2)servlet的实现

a)实现Servlet接口。

b)继承GenericServlet类

b)继承HttpServlet类

3)编写Servlet,实现一个Servlet接口

a)新建一个web 项目,并在SRC新建一个package,以及一个class,注意命名规范

package cn.piggy.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.sql.rowset.serial.SerialException;

public class HelloServlet implements Servlet{
public void destrory() {

}

public ServletConfig getServletConfig() {
return null;
}

public String getServletInfo() {
return null;
}

public void init(ServletConfig config) throws ServletException{

}

//处理请求
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException{
res.getOutputStream().print("Hello servlet!");
}

@Override
public void destroy() {
// TODO Auto-generated method stub

}


}

  

b)在web.xml中部署Servlet

c)发布

6 Servlet执行周期

因此:返回404错误时,需要检查路径是否输对,或者web.xml中存在该文件。

7 Servlet生命周期

 

猜你喜欢

转载自www.cnblogs.com/JohnTeslaaa/p/10205224.html