好久没有写servlet—jsp类型的web开发了,今天做了一个简单的开发流程。废话不多说上代码。
1. 创建一个web项目
2. 创建包:
3.在index.jsp页面增加一个表单
4.在com.arz.action包中创建一个action也就是一个servlet
5.创建dao
6.创建po
7.创建工具包,可以方便得到连接和关闭连接
8.做完这些别忘了导如Mysql的包
9.然后是配置web.xml
基本上代码写完了。现在我们要走一下servlet的请求过程
1. 首先是在index页面输入用户名和密码。
2. From发出请求(action=” addStudentAction”)
3. Web容器接受到请求后解析web.xml找到了url-pattern这个元素解析之后就可以找到对应的servlet类。在这个如果不太清楚web容器的可以看看我的“浅析Tomcat”这篇文章里面讲解了tomcat的原理。
4. 在servlet里面在去寻找对应的方法(get/post)
1. 创建一个web项目
2. 创建包:
3.在index.jsp页面增加一个表单
<h3>增加</h3>
<form action="addStudentAction" method="post">
用户名:<input type="text" name="addname"/>
密码:<input type="text" name="pwd"/>
<input type="submit" value="提交"/>
</form>
4.在com.arz.action包中创建一个action也就是一个servlet
package com.arz.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.arz.dao.StudentDao;
public class AddStudentAction extends HttpServlet {
StudentDao dao = new StudentDao();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name = req.getParameter("addname");
System.out.println(name);
String pwd = req.getParameter("pwd");
dao.add(name, pwd);
req.getRequestDispatcher("stu/ok.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}
}
5.创建dao
package com.arz.dao;
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.arz.po.Student;
import com.arz.util.JDBCUtil;
public class StudentDao {
private static Connection conn = null;
private static PreparedStatement ps = null;
public void add(String name , String pwd){
try{
conn = JDBCUtil.getConn();
String sql = "insert into student(name,pwd)values(?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, pwd);
ps.execute();
}catch(SQLException e){
e.printStackTrace();
}finally{
JDBCUtil.close(null, ps, conn);
}
}
public void del(String name){
try{
conn = JDBCUtil.getConn();
String sql = "delete from student where name = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, name);
System.out.println(ps.execute());
}catch(SQLException e){
e.printStackTrace();
}finally{
JDBCUtil.close(null, ps, conn);
}
}
public List<Student> sel(String name){
List<Student> list = new ArrayList<Student>();
ResultSet rs = null;
try{
conn = JDBCUtil.getConn();
String sql = "select name,pwd from student where name = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, name);
rs = ps.executeQuery();
while(rs.next()){
String sultName = rs.getString(1);
String sultPwd = rs.getString(2);
list.add(new Student(sultName,sultPwd));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
JDBCUtil.close(rs, ps, conn);
}
return list;
}
public static void main(String[] args){
StudentDao dao = new StudentDao();
List<Student> list = dao.sel("123123");
System.out.println(list.size());
for(int i = 0;i<list.size();i++){
System.out.println(list.get(i).getName());
}
}
}
6.创建po
package com.arz.po;
public class Student {
private int id ;
private String name ;
private String pwd;
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 getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Student(){}
public Student(String name,String pwd){
this.name = name;
this.pwd = pwd;
}
}
7.创建工具包,可以方便得到连接和关闭连接
package com.arz.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
public class JDBCUtil {
private static Connection conn;
private static String url = "jdbc:mysql://localhost:3306/test";
private static String user = "root";
private static String password = "root";
public static Connection getConn() {
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,user,password);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static void close(ResultSet rs , Statement sta , Connection conn ){
if(rs != null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(sta != null){
try{
sta.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
8.做完这些别忘了导如Mysql的包
9.然后是配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>student</servlet-name>
<servlet-class>com.arz.action.AddStudentAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>student</servlet-name>
<url-pattern>/addStudentAction</url-pattern>
</servlet-mapping>
</web-app>
基本上代码写完了。现在我们要走一下servlet的请求过程
1. 首先是在index页面输入用户名和密码。
2. From发出请求(action=” addStudentAction”)
3. Web容器接受到请求后解析web.xml找到了url-pattern这个元素解析之后就可以找到对应的servlet类。在这个如果不太清楚web容器的可以看看我的“浅析Tomcat”这篇文章里面讲解了tomcat的原理。
4. 在servlet里面在去寻找对应的方法(get/post)