java学习总结:JSP、Servlet
一、软件的结构
- 必须下载特定的客户端程序。
- 服务器端升级,客户端升级。
- 不需要安装特定的客户端(只需要安装浏览器即可!!)
- 服务器端升级,浏览器不需要升级!!!!
- WebLogic: BEA 公司的产品。 收费的。
- WebSphere : IBM公司的产品。收费的。
- JBoss: Redhat 公司的产品。收费的。
- Tomcat : 开源组织 Apache 的产品。免费的。
二、IDEA、Tomcat环境搭建
1、如何将项目变成Web项目:




2、配置tomcat



三、Servlet
1、什么是Servlet


2、Servlet生命周期
- 什么时候初始化
- 什么时候被调用
- 什么时候被销毁
- init方法:initial 创建完servlet对象时候调用。只调用1次。
- service:每次浏览器发出请求时候调用这个方法。调用n次。
- destory:销毁servlet对象的时候调用。停止服务器或者重新部署web应用时候会销毁servlet对象。只调用1次。
四、JSP
//http://localhost:8080/JavaWeb/index.jsp
//http://localhost:8080/JavaWeb/student
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
//默认访问service
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("StudentServlet.service");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<Student> list = new ArrayList<>();
try {
connection = JDBCUtil.getConnection();
String sql = "SELECT id,name,age,gender FROM student";
//预编译
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {//判断下一个有没有,如果返回true而且指向下一个,没有返回false
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String gender = resultSet.getString("gender");
Student student = new Student(id, name, age, gender);
list.add(student);
}
for (Student student : list) {
System.out.println(student);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtil.close(connection, preparedStatement, resultSet);
}
//把list数据放到req里面
req.setAttribute("list", list);
//转发到student_list.jsp页面进行展示
req.getRequestDispatcher("student_list.jsp").forward(req, resp);
}
}
<%@ page import="com.situ.web.pojo.Student" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="static/bootstrap-3.4.1-dist/css/bootstrap.css">
</head>
<body>
<%
//JSP页面中可以嵌套Java代码
//JSP脚本:在这里可以写任意的Java代码
//request、response:JSP页面的内置对象
List<Student> list = (List<Student>) request.getAttribute("list");
%>
<table class="table table-striped table-bordered table-hover table-condensed">
<tr>
<td>ID</td>
<td>名字</td>
<td>年龄</td>
<td>性别</td>
<td>编辑</td>
<td>删除</td>
</tr>
<%
for (Student student : list) {
%>
<tr>
<td><%=student.getId()%></td>
<td><%=student.getName()%></td>
<td><%=student.getAge()%></td>
<td><%=student.getGender()%></td>
<td>编辑</td>
<td>删除</td>
</tr>
<%
}
%>
</table>
</body>
</html>
五、URL路径
http://localhost:8080/JavaWeb/index.html?method=selectAll&id=2
http://localhost:8080/JavaWeb/index.jsp
http://localhost:8080/JavaWeb/student
http : 协议(http协议)
localhost:域名(为了方便记忆,最终定位地址还是要将域名转换为ip地址) local:本地 host:主机
本地域名:localhost 127.0.0.1
外部域名:www.baidu.com
8080:端口号
8080:tomcat默认端口
3306:mysql默认端口
JavaWeb:找到部署到webapps下面的JavaWeb这个应用
index.html:资源的名字
?后面是传递的参数
DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串。通过主机名,最终得到该主机名对应的IP地址的过程叫做域名解析(或主机名解析)。
张微博 weibo 微博
六、Tomcat
1、Tomcat 的目录结构
|-bin: 存放 tomcat 的命令。binary
startup.bat
shutdown.bat
|- conf : 存放 tomcat 的配置信息。其中 server.xml文件是核心的配置文件。 configuration
|-lib :支持 tomcat软件运行的 jar 包。其中还有技术支持包,如 servlet ,jsp lib: library
|-logs :运行过程的日志信息
|-temp: 临时目录 temp:temporary
|-webapps : 共享资源目录。 web 应用目录。 application 应用程序
|-work : tomcat的运行目录。 jsp 运行时产生的临时文件就存放在这里
2、项目部署
工作空间项目(写代码看到的) 和 tomcat部署的web项目是有差别的:
真正访问的是部署到tomcat的web项目下面的所有资源
tomcat部署的web项目:
--WEB-INF目录:
-- web.xml:web项目的核心配置文件
-- classes目录:放置字节码文件的目录(把src下面java代码编译后放到这个目录下面)
-- lib目录:放置依赖的jar包
--index.jsp
七、tomcat中文乱码问题
-Dfile.encoding=UTF-8