servlet基础,
servlet基础,
1.JAVA版本划分
JAVA 一门编程语言
JDK JAVA DEVELOPMENT KT java开发套件 javac[其中用于编译的工具] java【用于运行class文件工具】
JRE JAVA RUNTIME ENVIREMONT java运行环境 优化的jdk 只适合线上运行 不适合开发环境 没有javac
JAVASE Java Platform Standard Edition java基础 提供基础的语法支持,各种库(lang,util,net,io…) jdk和jre自带
JAVAEE Java Platform Enterprise Edition java企业级开发 jdk不带 sun公司提供了javaee的标准(定义了接口 jdbc crud) 未提供实现
接口层 SERVLET+JSP
数据访问层 JDO+JTA+JDBC+EJB
HTTP层 JAXWS+JAXRS
2 web服务器
nginx服务器:http://nginx.org/en/download.html
dos命令检查某个端口是否开放
C:\Users\Administrator>netstat -aon | find “80”
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 6052
80端口是web编程默认的端口 可以省略
3 http协议
浏览器中输入了某个地址访问时
请求:浏览器会打包格式化的数据给服务区
请求头(浏览器的信息 访问的地址 /t.html 支持的语言 告诉服务器我要什么)+请求体(带一些额外的数据给服务器 参数 文件)
请求头中【url,主机名称,参数,请求类型】
请求体中【参数,文件】
重点:
参数
GET请求参数 /
?UserName=yyy&Password=kkk get请求传参是直接在url末尾的,键值对 map
get请求在http和https中都是不安全 直接在路径上显示
浏览器输入
POST请求参数 参数在请求体中
UserName: aaa
Password: bbb
post请求 在http中是相对安全 在https是绝对安全
只有表单或者ajax可以设置post
响应:服务器接受到请求后 根据请求头中的地址和请求的方式(GET POST) 决定调用什么方法实现什么逻辑(编程)
处理逻辑完成后 打包一个格式化数据发送给浏览 响应头(服务器时间,服务器名称,服务器发送文件的编码) 响应体(html)
3 静态页面和动态页面
静态页面直接通过html编程方式写死的代码
1
2
…
动态页面就是可以根据自定义的逻辑生成html
for(int i=0;i<10;i++){
response.write("“+i+”")
}
nginx支持静态页面(static)
tomcat 支持动态和静态(dynamic)
动态技术:JAVAEE servlet+jsp
tomcat.apache.org 选择左侧的download 下载对应版本
浏览器清空缓存 ctrl+shift+delete
四 tomcat容器的目录结构
bin目录 可执行文件目录
conf 配置文件目录
server.xml 配置端口 虚拟目录
webapps 静态和动态页面的位置
work 缓存目录
eclipse只是一个编程工具 负责将编写好的代码 编译同步到tomcat的webapps目录
tomcat实际运行应用程序
package com.student.Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySQLUtil {
private static String url="jdbc:mysql://localhost:3306/unit02";
private static String username="test_4";
private static String password="123456";
public static Connection co=null;
public static Connection getConnectionInstance() throws ClassNotFoundException, SQLException {
if(null==co) {
Class.forName("com.mysql.jdbc.Driver");
co=DriverManager.getConnection(url,username,password);
return co;
}else {
return co;
}
}
}
package com.student.Util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.student.baen.StuAttribute;
public class StuMysql {
/**
* 查询所有学生数据
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public List stuSelect() throws ClassNotFoundException, SQLException {
Connection co=MySQLUtil.getConnectionInstance();
PreparedStatement ps=co.prepareStatement("SELECT s.sno,s.sex,s.name FROM student s");
//获取查询结果
ResultSet res=ps.executeQuery();
List<StuAttribute> stuGather=new ArrayList<StuAttribute>();
//解析结果集
while(res.next()) {
String sno=res.getString("sno");
String sex=res.getString("sex");
String name=res.getString("name");
StuAttribute stu=new StuAttribute();
stu.setSno(sno);
stu.setName(name);
stu.setSex(sex);
stuGather.add(stu);
}
ps.close();
return stuGather;
}
/**
* 新增学生信息
* @param stuArr
* @throws ClassNotFoundException
* @throws SQLException
*/
public void stuAdd(StuAttribute stuArr) throws ClassNotFoundException, SQLException {
Connection co=MySQLUtil.getConnectionInstance();
PreparedStatement ps=co.prepareStatement("INSERT into student (name,sex) VALUES(?,?)");
//设置SQL参数
ps.setString(1, stuArr.getName());
ps.setString(2, stuArr.getSex());
//执行SQL
ps.executeUpdate();
ps.close();
}
/**
* 根据sno删除学生信息
* @param sno
* @throws SQLException
* @throws ClassNotFoundException
*/
public void deleteStuInfo(String sno) throws ClassNotFoundException, SQLException {
Connection co=MySQLUtil.getConnectionInstance();
PreparedStatement ps=co.prepareStatement("DELETE FROM student WHERE sno=?");
ps.setString(1, sno);
//执行SQL
ps.executeUpdate();
ps.close();
}
/**
* 根据学生号修改
* @param stuArr
*/
public void updateStuInfo(StuAttribute stuArr) throws ClassNotFoundException, SQLException{
Connection co=MySQLUtil.getConnectionInstance();
PreparedStatement ps=co.prepareStatement("UPDATE student s set s.name=?,s.sex=? WHERE s.sno=?");
//设置SQL参数
ps.setString(1, stuArr.getName());
ps.setString(2, stuArr.getSex());
ps.setString(3, stuArr.getSno());
//执行SQL
ps.executeUpdate();
ps.close();
}
}
相关文章
- 暂无相关文章
用户点评