TomCat(Web服务器)
(一)Tomcat-->
:java-web-server-software(针对于JAVA平台)
结构:
1)bin目录
·停止服务·服务运行)
2)lib目录
:对Java程序的支持(提供Java运行所使用到的各类jar包)
3)conf目录
:服务器的配置文件所在目录
4)webapps目录
:程序项目发布目录
5)log目录
:程序运行过程中记录文件的日志文件
6)work目录
:应用程序的镜像备份目录。
(二)Tomcat和eclipse结合
事例:
创建index.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Susana</h1>
</body>
</html>
用IP地址去网页查询:
设置两个包:(1,com.yonyou.hi.entity class:Employee
2, com.yonyou.hi.common class:Common)
Employee:
package com.yonyou.hello1.entity;
import java.io.Serializable;
public class Employee implements Serializable {
private int id;
private String name;
private String phone;
private String gender;
public Employee(int id, String name, String phone, String gender) {
super();
this.id = id;
this.name = name;
this.phone = phone;
this.gender = gender;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
Common:
package com.yonyou.hello1.common;
import java.util.ArrayList;
import java.util.List;
import com.yonyou.hello1.entity.Employee;
public class Common {
public static List<Employee> getEmployee(){
List<Employee> emps = new ArrayList<Employee>();
emps.add(new Employee(1,"susana","123123123","nv"));
emps.add(new Employee(2,"daming","12313453","nv"));
emps.add(new Employee(3,"lingling","127893123","nv"));
emps.add(new Employee(4,"sam","123123787893","boy"));
return emps;
}
}
在创建list.jsp文件
<%@page import="com.yonyou.hello1.common.Common"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.yonyou.hello1.entity.Employee"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
List<Employee> emps = Common.getEmployee();
for(Employee emp:emps){
out.print(emp.getId()+","+emp.getName()+","+emp.getPhone()+","+emp.getGender()+"<hr />");
}
%>
</body>
</html>
最后启动服务 去网页查询:
**小结:
Java程序在Tomcat中运行原理:
1》publish前,Java原程序(.Java)文件会被编译成.class文件,并放在web--info/class下;
2》通过Tomcat当访问JSP文件时,那么Tomcat会对JSP文件进行编译,生成JSP对应的.class文件;
3》访问JSP时,Tomact会对JSP生成的.class文件进行调用,同时协调Web--info/class下的其他程序一同运行;
4》执行JSP后,运行结果会被Tomact生成一个HTML文件内容,并响应给请求客户端浏览器;
(二)JSP
* JSP运行在服务器端(Tomcat)*
1)JSP文件中
1,language 服务器语言
2,content type:指令
3,text/HTML文本
注意:要涉及到底层Java代码时要重启服务器;
事例:
创建index.jsp文件
:<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用户登录</h1>
<form action="login_sb_jsp.jsp" method = "post">//地址栏隐藏密码
用户名: <input type= "text" name = "txtUserName"/>
密码: <input type = "password" name ="txtxPassword"/>
<input type ="submit" value ="登录" />
</form>
</body>
</html>
去查询:
创建login_sb_jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//request是JSP的内置对象(可直接使用),getParameter()方法,接出前一个登陆页的请求;
* request.getParameter()*--》得到参数
String userName = request.getParameter("txtUserName");
String password = request.getParameter("txtPassword");
if(userName.equals("susana")&&password.equals("123456")){
out.print("登陆成功");//像画面中输出
}else{
out.print("登陆失败");
}
%>
查询:
else: