文章目录
JSP页面
JSP Scriptlets
<% java代码(变量、方法、表达式等) %>
JSP声明语句
<%!
定义的变量或方法等
%>
JSP表达式
<%= expression %>
JSP注释
<%-- 这是JSP注释 %>
page指令
<%@ page 属性名1="" 属性名2="" %>
常用:
language=“java”
contentType=“text/html;charset=utf-8”
pageEnCoding=“UTF-8”
import=“com.zhp.xxx”
include指令
静态包含:
<%@ include file="文件路径" %>
动态包含:
<jsp:include page="xxx.html" flush="true">
jsp转发
<jsp:forword page="页面地址">
JDBC
//注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//通过DriverManager获取数据库链接
String url = "jdbc:mysql://localhost:3306/jdbc";
String username = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url,username, password);
//通过Connection 获取Statement对象
Statement stmt = conn.createStatement();
//通过stmt执行sql语句
String sql = "select * from user";
//非查询语句不加Query
ResultSet rs = stmt.executeQuery(sql);
//查询结果集
while(rs.next()){
String name = rs.getString("name");
int age = rs.getInt("age");
}
Servlet
统一设置编码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置post页面编码为UTF-8
request.setCharacterEncoding("UTF-8");
//设置get页面编码为UTF-8
response.setContentType("text/html; charset=utf-8");
}
请求转发与重定向
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//转发
request.getRequestDispatcher("xxx.jsp").forword(request, response);
//重定向
response.sendRedirect("/xxxx");
}
请求域数据共享与获取表单数据
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//向请求域添加数据
request.addAttribute(String s, Object o);
//获取请求域数据
Object o = request.getAttribute(String name);
//获取表单数据
String s = request.getParamter(String name);
}
JavaBean
public class XXXX{
private String name;
private int age;
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
}