8.11学习心得
<% 代码片段 %> //JSP脚本
<%= 表达式 %> //JSP表达式
<!-- 注释 --%> //JSP注释
1. JSP 指令
(1)page 指令
设置jsp页面的静态属性
定义页面的依赖属性,比如脚本语言、error页面、缓存需求等 (用的比较多的有import)
(2)include 指令
向当前页面插入一个静态的文件(可以是jsp页面、html页面、文本或java程序)内容
include 指令是通知jsp容器把另外的文件包含到当前页面中
(3)taglib 指令
- 引入自定义的标签库(并且可以指定引入标签的前缀)
2. JSP 动作标记
<jsp:include>标签
<jsp:forward>标签
<jsp:param>标签
<jsp:plugin>标签
<jsp:include>与include指令的比较:
· <jsp:include>标签是动态引入, <jsp:include>标签涉及到的2个JSP页面会被翻译成2个servlet,这2个servlet的内容在执行时进行合并。
· 而include指令是静态引入,涉及到的2个JSP页面会被翻译成一个servlet,其内容是在源文件级别进行合并。
· <jsp:include>标签:使用page属性指定被引入资源。include指令:使用file属性指定被引入资源。
3. JSP 内置对象
(1)9种:
- request
- response
- out
- session
- application
- config
- pagecontext
- page
- exception
request.getParameter(String s); //获取对象
response.sendRedirect(URL url); //重定向
(2)session对象的生存期限
- 客户是否关闭浏览器。
- session对象是否调用invalidate()方法使session无效。
- session对象是否达到设置的最长“发呆”时间。
4. Java Servlet 基础
(1)Servlet 与 JSP 的差别
- Servlet:Java代码中输出HTML
- JSP:HTML中嵌入Java代码
(2)有几种方式实现 Servlet?
- 实现 Servlet 接口
Servlet类(重写service方法)+web.xml配置 - 继承 GenericServlet
Servlet类(重写service方法)+web.xml配置 - 继承 HttpServlet
Servlet类(重写doGet、doPost方法)+web.xml配置
(3)常用servlet开发步骤
- Web工程
- 新建类,继承HttpServlet类
- 覆盖doGet()或者doPost()方法
通过HttpServletRequest和HttpServletResponse接口获取用户数据,并向用户返回数据。(包含在第三步) - 映射,配置web.xml文件
5. MVC 模式
MVC模式的核心思想是有效地组合“视图”、“模型”和“控制器”。
6. 登录界面
db.properties
Driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/itcast
username=root
password=123456
JDBCUtil 类:
private static String driver;
private static String url;
private static String username;
private static String password;
static{
try{
Properties p = new Properties();
p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
driver = p.getProperty("Driver");
url = p.getProperty("url");
username = p.getProperty("username");
password = p.getProperty("password");
Class.forName(driver);
} catch(IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection CreateConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, Statement stmt, ResultSet rs){
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Connection conn,PreparedStatement stmt) {
try {
if(stmt != null){
stmt.close();
}
if(conn != null){
conn.close();
}
}catch (SQLException e) {
e.printStackTrace();
}
}
person 类:
private String name;
private String password;
private String tel;
private String em;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEm() {
return em;
}
public void setEm(String em) {
this.em = em;
}
public person() {
}
public person(String name, String password, String tel, String em) {
this.name = name;
this.password = password;
this.tel = tel;
this.em = em;
}
@Override
public String toString() {
return "person [name=" + name + ", password=" + password + ", tel=" + tel + ", em=" + em + "]";
}
index.jsp
<body>
<form action="HelloSS" method="post">
姓名:<input type="text" name="na" /><br />
密码:<input type="password" name="password" /><br />
电话:<input type="text" name="tel" /><br />
邮箱:<input type="text" name="em" /><br />
<input type="submit" value="登录" />
<input type="reset" value="重置" />
</form>
</body>
HelloSL 类:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("na");
String password = request.getParameter("password");
String tel = request.getParameter("tel");
String em = request.getParameter("em");
name = new String(name.getBytes("ISO-8859-1"),"utf-8");
password = new String(password.getBytes("ISO-8859-1"),"utf-8");
tel = new String(tel.getBytes("ISO-8859-1"),"utf-8");
em = new String(em.getBytes("ISO-8859-1"),"utf-8");
person person1 = new person(name, password, tel, em);
//response.setContentType("text/html;charset=utf-8");
Connection conn = JDBCUtil.CreateConnection();
StringBuffer sql = new StringBuffer();
sql.append( "insert into person values(").append(0).append(",");
sql.append("'").append(name).append("',");
sql.append("'").append(password).append("',");
sql.append("'").append(tel).append("',");
sql.append("'").append(em).append("')");
System.out.println("sql: "+sql.toString());
PreparedStatement psts =null;
try {
psts = conn.prepareStatement(sql.toString());
psts.execute();
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBCUtil.close(conn, psts);
}
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(person1.toString());
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
MySQL person表设计:
结果: