WEB
获取表单提交数据
request.getParameter();
给网页作出响应
response.getWriter().write();
重定向
response.sendRedirect();
转发
request.setAttribute("","");
request.getRequestDispatcher("").forward(request,response);
转发的时候路径不加/ 重定向必须加
处理请求乱码
request.setCharacterEcoding("utf-8");
处理响应乱码
response.setCharacterEcoding("utf-8");
response.setContentType("text/html;charset=utf-8");
JSP
三大指令
1`page
2`include
3`taglib
九大内置对象
1`request
2`response
3`session
4`application
5`pageContext
6`page
7`out
8`exception
9`config
四大域对象
1`pageContext
2`request
3`session
4`applicatoin
六大动作标签
1`forward
2`include
3`param
4`useBean
5`setProperty
6`getProperty
login界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<h3>登录界面</h3>
<form action="/demo11_exam/login" method="post">
账号:<input type="text" name="zhang"><br>
密码:<input type="text" name="mima"><br>
<hr>
<input type="submit" value="登录">
</form>
${sb}
</center>
</body>
</html>
LoginServlet
package com.www.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Driver;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//处理乱码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取表单数据
String zhangs = request.getParameter("zhang");
String mimas = request.getParameter("mima");
//JDBC
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yyy", "root", "root");
String sql = "select * from t_temp where id=? and password=?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, zhangs);
pst.setString(2, mimas);
ResultSet rs = pst.executeQuery();
if(rs.next()){
System.out.println("登录成功");
response.sendRedirect("/demo11_exam/show");
}else{
System.out.println("登录失败");
request.setAttribute("sb", "账号或密码错误,请重新输入");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
ShowServlet
package com.www.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.www.bean.Elep;
public class ShowServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//处理乱码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//JDBC
List list = new ArrayList();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yyy", "root", "root");
String sql = "select * from t_temp ;";
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while(rs.next()){
String ids = rs.getString(1);
String names = rs.getString(2);
String passs = rs.getString(3);
String sexs = rs.getString(4);
String ages = rs.getString(5);
Elep el = new Elep(ids,names,passs,sexs,ages);
list.add(el);
}
request.setAttribute("list00", list);
request.getRequestDispatcher("/show.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
show界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'show.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<h2>This is show page</h2>
<table border="2px">
<tr>
<td>
账号
</td>
<td>
姓名
</td>
<td>
密码
</td>
<td>
性别
</td>
<td>
年龄
</td>
<td>
<a href="/demo11_exam/add.jsp">添加</a>
</td>
</tr>
<c:forEach var="li" items="${list00}">
<tr>
<td>
${li.id}
</td>
<td>
${li.name}
</td>
<td>
${li.password}
</td>
<td>
${li.sex}
</td>
<td>
${li.age}
</td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>
add界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'add.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<form action="/demo11_exam/add" method="post">
账号:<input type="text" name="hao"><br>
姓名:<input type="text" name="ming"><br>
密码:<input type="text" name="mima"><br>
性别:<input type="text" name="bie"><br>
年龄:<input type="text" name="nian"><br>
<input type="submit" value="提交">
</form>
</center>
</body>
</html>
AddServlet
package com.www.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//处理乱码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//获取表单数据
String haos = request.getParameter("hao");
String mings = request.getParameter("ming");
String mimas = request.getParameter("mima");
String bies = request.getParameter("bie");
String nians = request.getParameter("nian");
//JDBC
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yyy", "root", "root");
String sql = "insert into t_temp values(?,?,?,?,?);";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, haos);
pst.setString(2, mings);
pst.setString(3, mimas);
pst.setString(4, bies);
pst.setString(5, nians);
int x = pst.executeUpdate();
if(x>0){
System.out.println("添加成功");
response.sendRedirect("/demo11_exam/show");
}else{
response.sendRedirect("/demo11_exam/error.jsp");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
error界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'error.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<h2>添加失败</h2>
</center>
</body>
</html>
Elep对象
package com.www.bean;
public class Elep {
String id;
String name;
String password;
String sex;
String age;
public Elep(String id, String name, String password, String sex, String age) {
super();
this.id = id;
this.name = name;
this.password = password;
this.sex = sex;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
万物皆下品,惟有读书高