纯jsp实现简单的个人博客

1、创建数据库连接
package util;
import java.sql.Connection;
import java.sql.DriverManager;
public class jdbc {
 public static Connection getConn() throws Exception
 {
  String url="jdbc:mysql://127.0.0.1:3306/user";
  String username="root";
  String password="123456";
  Class.forName("com.mysql.jdbc.Driver");       //加载数据库驱动
  return DriverManager.getConnection(url,username,password);
 }
}

2.博客的bean
package bean;
public class Boke {
 private int id;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 private String title;
 private String content;
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
}

3.用户的bean
package bean;
public class User {
 private int id;
 private String username;
 private String password;
 private String shiming;
 private String hangye;
 private String zhiwei;
 private String xingbie;
 private String shengri;
 private String diqu;
 private String jianshu;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getShiming() {
  return shiming;
 }
 public void setShiming(String shiming) {
  this.shiming = shiming;
 }
 public String getHangye() {
  return hangye;
 }
 public void setHangye(String hangye) {
  this.hangye = hangye;
 }
 public String getZhiwei() {
  return zhiwei;
 }
 public void setZhiwei(String zhiwei) {
  this.zhiwei = zhiwei;
 }
 public String getXingbie() {
  return xingbie;
 }
 public void setXingbie(String xingbie) {
  this.xingbie = xingbie;
 }
 public String getShengri() {
  return shengri;
 }
 public void setShengri(String shengri) {
  this.shengri = shengri;
 }
 public String getDiqu() {
  return diqu;
 }
 public void setDiqu(String diqu) {
  this.diqu = diqu;
 }
 public String getJianshu() {
  return jianshu;
 }
 public void setJianshu(String jianshu) {
  this.jianshu = jianshu;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 @Override
 public String toString() {
  return "User [username=" + username + ", password=" + password + "]";
 }
}

4.Userdao接口
package dao;
import java.util.List;
import bean.User;
public interface Userdao {
 public boolean insert(User user);
 public User query(String username);
 public int query2(User user);   //0登陆成功,1用户不存在,2密码错误,3获取异常;
}

5.Userdaoimpl实现Userdao
package dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import bean.User;
import dao.Userdao;
import util.jdbc;
public class Userdaoimpl implements Userdao{
 
 private Connection conn=null;
 private PreparedStatement pst = null;
 private ResultSet rs = null;
 
 public boolean insert(User user) {
  
  try {
   conn=jdbc.getConn();
   String sql="insert into user(username, password) values (?, ?)";
   pst=conn.prepareStatement(sql);
   pst.setString(1, user.getUsername());
   pst.setString(2, user.getPassword());
   int i=pst.executeUpdate();
   if(pst!=null)
    pst.close();
   if(conn!=null)
    conn.close();
   if(i==1)
    return true;
   else
    return false;
  } catch (Exception e) {
   e.printStackTrace();
  }
  return false;
 }
 public User query(String username) {
  User user=new User();
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    user.setUsername(rs.getString("username"));
    user.setPassword(rs.getString("password"));
   }
   if(rs!=null)
    rs.close();
   if(pst!=null)
    pst.close();
   if(conn!=null)
    conn.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return user;
 }
 public int query2(User user) {
  int i = 3;
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, user.getUsername());
   System.out.println(user.getUsername());
   rs=pst.executeQuery();
   if(rs.next())
   {
    if(!rs.getString("password").equals(user.getPassword()))
     i= 2;
    else
     i= 0;
   }
   else
   {
    i= 1;
   }
   if(rs!=null)
    rs.close();
   if(pst!=null)
    pst.close();
   if(conn!=null)
    conn.close();
  }catch (Exception e) {
   e.printStackTrace();
  }
  return i;
 }
}

6.Userservice接口
package service;
import bean.User;
public interface Userservice {
 public boolean insert(User user);
 public User query(String username);
 public int query2(User user);
}

7.Userserviceimpl实现Userservice接口
package service.impl;
import bean.User;
import dao.Userdao;
import dao.impl.Userdaoimpl;
import service.Userservice;
public class Userserviceimpl implements Userservice{
 private Userdao userdao;
 
 public Userserviceimpl()
 {
  userdao=new Userdaoimpl();
 }
 public boolean insert(User user) {
  
  return userdao.insert(user);
 }
 public User query(String username) {
  // TODO Auto-generated method stub
  return userdao.query(username);
 }
 public int query2(User user) {
  // TODO Auto-generated method stub
  return userdao.query2(user);
 }
 
}

8.方法类,嫌麻烦没写在Userdao里
package moth;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
import bean.Boke;
import bean.User;
import util.jdbc;
public class method
{
 private Connection conn=null;
 private PreparedStatement pst = null;
 private ResultSet rs = null;
 public String hangye(String username) throws Exception
 {
  conn=jdbc.getConn();
  String sql="select * from user where username=?";
  pst=conn.prepareStatement(sql);
  pst.setString(1, username);
  rs=pst.executeQuery();
  if(rs.next())
  {
   String hangye=rs.getString("hangye");
   if(hangye==null||hangye.equals(""))
    return "未填写行业";
   else
    return rs.getString("hangye");
  }
  else
  {
   return "获取数据异常";
  }
 }
 public String zhiwei(String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    String zhiwei=rs.getString("zhiwei");
    if(zhiwei==null||zhiwei.equals(""))
     return "未填写职位";
    else
     return rs.getString("zhiwei");
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return "获取数据异常";
 }
 public String shiming(String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    String shiming=rs.getString("shiming");
    if(shiming==null||shiming.equals(""))
     return "未填写姓名";
    else
     return rs.getString("shiming");
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return "获取数据异常";
 }
 public String xingbie(String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    String xingbie=rs.getString("xingbie");
    if(xingbie==null||xingbie.equals(""))
     return "未填写性别";
    else
     return rs.getString("xingbie");
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return "获取数据异常";
 }
 public String shengri(String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    String shengri=rs.getString("shengri");
    if(shengri==null||shengri.equals(""))
     return "未填写生日";
    else
     return rs.getString("shengri");
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return "获取数据异常";
 }
 public String jianshu(String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    String jianshu=rs.getString("jianshu");
    if(jianshu==null||jianshu.equals(""))
     return "未填写简述";
    else
     return rs.getString("jianshu");
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return "获取数据异常";
 }
 public String diqu(String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    String diqu=rs.getString("diqu");
    if(diqu==null||diqu.equals(""))
     return "未填写地区";
    else
     return rs.getString("diqu");
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return "获取数据异常";
 }
 public ArrayList title(String username)
 {
  ArrayList titlelist=new ArrayList();
  try {
   conn=jdbc.getConn();
   String sql="select * from article where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   while(rs.next())
   {
    Boke boke=new Boke();
    boke.setId(rs.getInt("id"));
    boke.setTitle(rs.getString("title"));
    boke.setContent(rs.getString("content"));
    titlelist.add(boke);
   }
   if(rs!=null)
    rs.close();
   if(pst!=null)
    pst.close();
   if(conn!=null)
    conn.close();
   
  } catch (Exception e) {
   e.printStackTrace();
  }
  return titlelist;
 }
 public Boke query(int id){
  
  try {
   conn=jdbc.getConn();
   String sql="select * from article where id=?";
   pst=conn.prepareStatement(sql);
   pst.setInt(1, id);
   rs=pst.executeQuery();
   
   rs.next();
   Boke boke = new Boke();
   boke.setTitle(rs.getString("title"));
   boke.setContent(rs.getString("content"));
   return boke;
  }catch(Exception e){
   e.printStackTrace();
  }
  return null;
 }
 
 public void edit(Boke boke){
  
  try {
   conn=jdbc.getConn();
   String sql="update article set title=?,content=? where id=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, boke.getTitle());
   pst.setString(2, boke.getContent());
   pst.setInt(3, boke.getId());
   pst.executeUpdate();
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public void delete(int id){ 
   try {
    conn=jdbc.getConn();
    String sql="delete from article where id=?";
    pst=conn.prepareStatement(sql);
    pst.setInt(1, id);
    pst.executeUpdate();
   }catch(Exception e){
    e.printStackTrace();
   }
  }
 public void save(String username,String title,String content)
 {
  try{
   conn=jdbc.getConn();
   String sql="insert into article(username,title,content) values (?, ?,?)";
   pst=conn.prepareStatement(sql);
   pst.setString(1,username);
   pst.setString(2, title);
   pst.setString(3, content);
   pst.executeUpdate();
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 public void update(User user,String username1){
  try{
   conn=jdbc.getConn();
   String sql="update user set username=?,shiming=?,hangye=?,zhiwei=?,xingbie=?,shengri=?,diqu=?,jianshu=? where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1,user.getUsername());
   pst.setString(2, user.getShiming());
   pst.setString(3, user.getHangye());
   pst.setString(4, user.getZhiwei());
   pst.setString(5, user.getXingbie());
   pst.setString(6, user.getShengri());
   pst.setString(7, user.getDiqu());
   pst.setString(8, user.getJianshu());
   pst.setString(9, username1);
   pst.executeUpdate();
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 public User saveuser(User user,String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from article where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   
   String hangye=this.hangye(username);
   String zhiwei=this.zhiwei(username);
   String xingming=this.shiming(username);
   String xingbie=this.xingbie(username);
   String shengri=this.shengri(username);
   String jianshu=this.jianshu(username);
   String diqu=this.diqu(username);
   user.setHangye(hangye);
   user.setZhiwei(zhiwei);
   user.setXingbie(xingbie);
   user.setShiming(xingming);
   user.setShengri(shengri);
   user.setJianshu(jianshu);
   user.setDiqu(diqu);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return user;
 }
}

9.登陆的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>账号登录</title>
<style type="text/css">
  #second{
   margin-top: 11%;
   margin-left:43%;
   font-size:24px;
  }
  #third{
   margin-top:5%;
  }
 </style>
<script>
 function check()
 {
  var username=document.getElementById("username");       //得到id为name元素的值
  var userpwd=document.getElementById("password");
  if(username.value=="")
   {
    alert("请输入用户名");      //弹出一个对话框
    username.focus();          //让光标显示在用户名那一行
    return false;
   }else if(username.value.length>16||username.value.length<6)
    {
     alert("用户名长度不符合规则!");
     username.focus();
     return false;
    }
    else
    {
     if(userpwd.value=="")
     {
      alert("请输入密码");
      userpwd.focus();       //让光标显示在密码那一行
      return false;
     }else if(userpwd.value.length<6)
     {
      alert("密码长度不能小于六位");
      userpwd.focus();
      return false;
     }
     else
     {
      for(var i=0;i<username.value.length;i++)
      {
       var charTest=username.value.toLowerCase().charAt(i);
       if((!(charTest>='0'&&charTest<='9'))&&(!(charTest>='a'&&charTest<='z')))
        {
         alert("用户名含有非法字符!");
         username.focus();
         return false;
        }
      }
     }
    }
  return true;
 }
 </script>
</head>
<body style="background-image: url('image/2.bmp');text-align: center">
 <div>
 <form style="font-family:楷体;" method="post" action="${pageContext.request.contextPath }/indexservlet" onSubmit="return check()">
   <div id="third">
   <h1>账号登陆</h1>
   </div>
   <div id="second">
   <table cellpadding="0" cellspacing="0" border="0">
    <tr>
     <td>用户名:</td>
     <td><input type="text" id="username" name="username"></td>
    </tr>
    <tr>
     <td>密&nbsp;&nbsp;码:</td>
     <td><input type="password" id="password" name="password"></td>
    </tr>
    <tr>
     <td>&nbsp;</td>
    </tr>
    <tr>
     <td colspan="2" align="center"><input type="submit" style="background:url('image/3.png');width: 60px;height: 30px;border: 0"  value="登陆"></td>
    </tr>
   </table>
   
   </div><br/>
      <b>温馨提示:</b><br/><br/>
      <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;还没有账号?</b>
                  <a href="regist.jsp">立即注册</a>
  </form>
  </div>
</body>
</html>

10.注册的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>账号注册</title>
<style type="text/css">
 .bg
 {
        z-index:-1;  /*使背景图片置底显示*/
        position:absolute;/*将背景图片设置成 absolute(绝对定位),使其显示于文档流之下,不占有文档流空间*/ 
        left:250px;
        top:80px;
 }
 #size
 {
  width:280px;
     height:50px;
     font-size:20px;
     background:red;
 }
 #tishi{
  color:green;
  font-size:15px;
 }
</style>
<script>
function check()
{
 var username=document.getElementById("username");       //得到id为name元素的值
 var userpwd=document.getElementById("password");
 var userpwd2=document.getElementById("repassword");
 if(username.value=="")
  {
   alert("请输入用户名");      //弹出一个对话框
   username.focus();          //让光标显示在用户名那一行
   return false;
  }else if(username.value.length>16||username.value.length<6)
   {
    alert("用户名长度不符合规则!");
    username.focus();
    return false;
   }
   else
   {
    if(userpwd.value=="")
    {
     alert("请输入密码");
     userpwd.focus();       //让光标显示在密码那一行
     return false;
    }
    else if(userpwd2.value=="")
    {
     alert("请输入确认密码");
     userpwd2.focus();       //让光标显示在密码那一行
     return false;
    }
    else if(userpwd.value.length<6)
    {
     alert("密码长度不能小于六位");
     userpwd.focus();
     return false;
    }
    else
    {
     for(var i=0;i<username.value.length;i++)
     {
      var charTest=username.value.toLowerCase().charAt(i);
      if((!(charTest>='0'&&charTest<='9'))&&(!(charTest>='a'&&charTest<='z')))
       {
        alert("用户名含有非法字符!");
        username.focus();
        return false;
       }
     }
    }
   }
 return true;
}
</script>
</head>
<body>
 <form style="font-family:楷体;" method="post" action="${pageContext.request.contextPath }/registsevlet" onSubmit="return check()">
   <img src=image/5.jpg class="bg" alt="图片无法加载">
   <table style="position: absolute;left: 1000px;top: 120px;border:1px solid gray" width="300" cellspacing="0">
    <tr height="15"></tr>
    <tr>
     <td colspan="2" align="left"><h1>账号注册</h1></td>
    </tr>
    <tr>
     <td>
      <h2>用户名:</h2>
     </td>
     <td>
      <input type="text" id="username" name="username" align="left" style="height:25px;width:180px;">
      <span id="tishi">(由6-16位字母和数字组成)</span>
     </td>
    </tr>
    <tr>
     <td>
      <h2>密&nbsp;&nbsp;码:</h2>
     </td>
     <td>
      <input type="password" id="password" name="password" align="left" style="height:25px;width:180px;">
      <span id="tishi">(密码长度不能低于6位)</span>
     </td>
    </tr>
    <tr>
     <td>
      <h3>确认密码:</h3>
     </td>
     <td>
      <input type="password" id="repassword" name="repassword" align="left" style="height:25px;width:180px;">
      <span id="tishi">(密码长度不能低于6位)</span>
     </td>
    </tr>
    <tr height="15"></tr>
    <tr>
     <td align="center" colspan="2">
      <input type="submit" value="注册" id="size">&nbsp;&nbsp;&nbsp;
     </td>
    </tr>
    <tr height="2"></tr>
   </table>
  </form>
</body>
</html>

11.登录的servlet
package sevlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import bean.User;
import moth.method;
import service.Userservice;
import service.impl.Userserviceimpl;
public class indexservlet extends HttpServlet {
 Userservice userservice=null;
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doPost(request, response);
 }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setCharacterEncoding("UTF-8");
  PrintWriter out=response.getWriter();
  userservice=new Userserviceimpl();
  String username=request.getParameter("username");
  String password=request.getParameter("password");
  request.getSession().setAttribute("username",username);
  request.getSession().setAttribute("password",password);
  
  User user = new User();
  user.setUsername(username);
  user.setPassword(password);
  switch(userservice.query2(user))
  {
   case 0:
    method md = new method();
    user=md.saveuser(user, username);
    List bokelist = md.title(username);
    request.setAttribute("bokelist", bokelist);
    request.getSession().setAttribute("user", user);
    //request.getRequestDispatcher("1.jsp").forward(request, response);
    request.getRequestDispatcher("WEB-INF/jsp/page.jsp").forward(request, response);
    break;
   case 1:
    JOptionPane.showMessageDialog(null,"该用户不存在","提示消息",JOptionPane.WARNING_MESSAGE);
    request.getRequestDispatcher("index.jsp").forward(request, response);
    break;
   case 2:
    JOptionPane.showMessageDialog(null,"密码错误","提示消息",JOptionPane.WARNING_MESSAGE);
    request.getRequestDispatcher("index.jsp").forward(request, response);
    break;
   case 3:
    JOptionPane.showMessageDialog(null,"当前网页异常,请刷新该页面","提示消息",JOptionPane.WARNING_MESSAGE);
    request.getRequestDispatcher("index.jsp").forward(request, response);
    break;
  }
 }
}

12.注册的servlet
package sevlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import bean.User;
import service.Userservice;
import service.impl.Userserviceimpl;
/**
 * Servlet implementation class registsevlet
 */
public class registsevlet extends HttpServlet {
 private Userservice userservice=null;
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  userservice=new Userserviceimpl();
  String username=request.getParameter("username");
  String password=request.getParameter("password");
  String repassword=request.getParameter("repassword");
  
  User user=new User();
  user.setUsername(username);
  user.setPassword(repassword);
  
  if(!password.equals(repassword))
  {
   JOptionPane.showMessageDialog(null,"两次密码不一致","提示信息",JOptionPane.WARNING_MESSAGE);
   request.getRequestDispatcher("regist.jsp").forward(request, response);
  }
  else if(userservice.query(username).getUsername()==null)
  {
   if(userservice.insert(user)==true)
   {
    JOptionPane.showMessageDialog(null,"注册成功","提示信息",JOptionPane.WARNING_MESSAGE);
    request.getRequestDispatcher("index.jsp").forward(request, response);
   }
   else
   {
    JOptionPane.showMessageDialog(null,"注册失败,请重新注册","提示信息",JOptionPane.WARNING_MESSAGE);
    request.getRequestDispatcher("regist.jsp").forward(request, response);
   }
  }
  else if(userservice.query(username).getUsername()!=null)
  {
   JOptionPane.showMessageDialog(null,"该用户已存在","提示信息",JOptionPane.WARNING_MESSAGE);
   request.getRequestDispatcher("regist.jsp").forward(request, response);
  }
 }
}

13.首页的jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="moth.method,java.util.*,bean.Boke"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
<%
 String username=request.getParameter("username");
 method md=new method();
 String hangye=md.hangye(username);
 String zhiwei=md.zhiwei(username);
 String xingming=md.shiming(username);
 String xingbie=md.xingbie(username);
 String shengri=md.shengri(username);
 String jianshu=md.jianshu(username);
 String diqu=md.diqu(username);
%>
<style>
#homepage{
 margin:0;
 width:100%px;
 height:30px;
 background-color:#E8CCFF;
}
#data
{
 margin:0;
  width:30%px;
  height:120px;
  background-color:#FFFFFF;
}
</style>
</head>
<body>
 <div id="homepage">
  <h3 style="position: absolute;left:100px;top:-8px;">个人主页</h3>
 </div>
    <div id="data">
     <table width="1200px" border="0" style="position: absolute;left:80px;" height="100px" cellpadding="0" cellspacing="0">
     <tr height="60px">
      <td width="100%" style="position: absolute;left:0px;" align="center">
       <h2 style="color:red;">${username }</h2>
      </td>
     </tr>
     <tr>
      <td width="18%" height="20px">
       <h3>${user.hangye }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;</h3>
      </td>
      <td width="18%" height="20px">
       <h3>${user.zhiwei }&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;</h3>
      </td>
      <td width="18%" height="20px">
       <h3>${user.shiming }&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;</h3>
      </td>
      <td width="18%" height="20px">
       <h3>${user.xingbie }&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;</h3>
      </td>
      <td width="18%" height="20px">
       <h3>${user.shengri }</h3>
      </td>
      <td width="18%" height="20px">
       <a href="${pageContext.request.contextPath }/change?action=message"><h3>编辑资料</h3></a>
      </td>
     </tr>
     </table>
    </div>
   
    <table width="1200px" border="1" style="position: absolute;left:60px;" cellpadding="0" cellspacing="0">
     <tr>
      <td width="100%" height="48px" align="center valign="top">
       <table width="1200px" style="position: absolute;top:0px;" cellpadding="0" cellspacing="0"  border="0">
        <tr>
         <td>
          <input type="button" value="我的博客" onclick="document.getElementById('one').style.display='',document.getElementById('two').style.display='none',document.getElementById('three').style.display='none'" style="font-size:25px;background-color:#A9A9A9;width:400px;height:50px"/>
         </td>
         <td>
          <input type="button" value="写博客" onclick="document.getElementById('two').style.display='',document.getElementById('one').style.display='none',document.getElementById('three').style.display='none'" style="font-size:25px;background-color:#A9A9A9;width:400px;height:50px"/>
         </td>
         <td>
          <input type="button" value="我的资料" onclick="document.getElementById('three').style.display='',document.getElementById('one').style.display='none',document.getElementById('two').style.display='none'" style="font-size:25px;background-color:#A9A9A9;width:400px;height:50px"/>
         </td>
        </tr>
       </table>
      </td>
     </tr>
     <tr>
      <td width="100%" valign="top">
       <div id="one" style="display:"";">
       
       <table width="1199px" style="position: absolute;top:60px;background-color:#FAFAD2;" cellpadding="0" cellspacing="0"  border="0">
        <tr style="height:50px;" align="center">
         <td width="50%">标题</td>
         <td>操作</td>
        </tr>
        <c:forEach items="${bokelist }" var="boke">
        <tr style="height:50px;">
         <td align="center">${boke.title }</td>
         <td align="center"><a href="${pageContext.request.contextPath }/change?action=look&id=${boke.id }">查看</a>
          <a href="${pageContext.request.contextPath }/change?action=edit&id=${boke.id }&username=${user.username }&password=${user.password }">编辑</a>
          <a href="${pageContext.request.contextPath }/change?action=delete&id=${boke.id }">删除</a>
         </td>
        </tr>
        </c:forEach>
        <tr>
         <td>
          <br><br>
         </td>
        </tr>
       </table>
       
       
       </div>
       <div id="two" style="display:none">
     <form action="${pageContext.request.contextPath }/XieSevlet" method="post">
                           <table  width="1195px" style="position: absolute;top:60px;background-color:#FAFAD2;" cellpadding="0" cellspacing="0"  border="0">
                               <tr>
                                   <td width="20%" height="70px;" align="center">
                                       <span style="font-size:20px"><b>标题</b></span>
                                   </td>
                               <td>
                                   <input type="text" size="100" id="title" name="title" style="font-size:20px;width:1110px;height:40px;">
                               </td>
                               </tr>
                               <tr>
                                   <td width="20%" align="center">
                                    <span style="font-size:20px"><b>正文</b></span>
                                   </td>
                                   <td>
                                       <textarea id="content" name="content" rows="30" cols="110" style="font-size:15px;"></textarea>
                                   </td>
                               </tr>
                               <tr>
                                <td>
                                    <input type="submit" value="提交" style="position:absolute;top:620px;left:500px;width:100px;heigh:50px;font-size:20px;">
                                    <br><br><br><br>
                                </td>
                               </tr>
                           </table>
                           </form>
                 </div>
                 <div id="three" style="display: none">
     <form>
      <table width="1195px" style="position: absolute;top:60px;background-color:#FAFAD2;" cellpadding="0" cellspacing="10"  border="0">
       <tr>
        <td align="center">
         <h1>资料简介</h1><hr>
       </tr>
       <tr>
        <td align="center">
         <span style="color:red;">*</span>昵称:<input type="text" name="username" value=<%=username %> size="20">
         实名:<input type="text" id="shiming" name="shiming" value=<%=xingming %> size="20">
        </td>
       </tr>
       <tr>
        <td align="center">
         地区:<input type="text" name="diqu" value=<%=diqu %>>
         职位:<input type="text" id="zhiwei" name="zhiwei" value=<%=zhiwei %> size="20">
        </td>
       </tr>
       <tr>
        <td align="center">
         性别:<input type="text" name="xingbie" value=<%=xingbie %> size="20">
         生日:<input type="text" id="shengri" name="shengri" value=<%=shengri %> size="20">
         行业:<input type="text" id="shengri" name="shengri" value=<%=hangye %> size="20">
        </td>
       </tr>
       <tr>
        <td align="center">
         简述:<textarea id="content" name="jianshu" rows="30" cols="80" style="font-size:15px;"><%=jianshu %></textarea><br><br><br>
        </td>
       </tr>
      </table>
     </form>
                 </div>
      </td>
     </tr>
    </table>
</body>
</html>

14.编辑资料的jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>编辑资料</title>
</head>
<body style="background-image: url('image/4.jpg');text-align: center">
 <form action="${pageContext.request.contextPath }/ZiliaoSevlet">
  <table border="0" cellpadding="0" cellspacing="10" align="center">
   <tr>
    <td align="left">
     <h1>编辑简介</h1><hr><br>
    </td>
   </tr>
   <tr>
    <td align="center">
     <span style="color:red;">*</span>昵称:<input type="text" name="username" value=${user.username } size="20">
     实名:<input type="text" id="shiming" name="shiming" value=${user.shiming } size="20">
    </td>
   </tr>
   <tr>
    <td align="center">
     地区:<input type="text" name="diqu" value=${user.diqu }>
     职位:<input type="text" id="zhiwei" name="zhiwei" value=${user.zhiwei } size="20">
     性别:<input type="radio" name="xingbie" value="男"/>男
      <input type="radio" name="xingbie" value="女"/>女</br>
    </td>
   </tr>
   <tr>
    <td align="center">
     生日:<input type="text" id="shengri" name="shengri" value=${user.shengri } size="50">
     行业:<select name="hangye" id="行业">
       <option>医生</option>
       <option>护士</option>
       <option>程序员</option>
      </select></br>
    </td>
   </tr>
   <tr>
    <td align="center">
     简述:<textarea id="content" name="jianshu" rows="30" cols="100" style="font-size:15px;">${user.jianshu }</textarea>
    </td>
   </tr>
   <tr>
    <td align="center">
     <input type="submit" value="提交" style="background:#7FFFD4;width: 100px;height: 35px;"><br>
    </td>
   </tr>
  </table>
 </form>
</body>
</html>

15.编辑资料、查看文章、编辑文章的中间servlet
package sevlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import bean.Boke;
import moth.method;
public class change extends HttpServlet {
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doPost(request, response);
  
 }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String action=request.getParameter("action");
  if(action==null)
    action="";
  if(action.equals("look"))
   look(request,response);
  if(action.equals("edit"))
   edit(request,response);
  if(action.equals("delete"))
   delete(request,response);
  if(action.equals("message"))
   message(request,response);
 }
 protected void look(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  HttpSession session=request.getSession();
  String Sid=request.getParameter("id");
  int id=0;
  if(Sid==null||Sid.equals(""));
   Sid="0";
  try{
   id=Integer.parseInt(Sid);
  }catch(NumberFormatException e)
  {
   id=0;
   e.printStackTrace();
  }
  ArrayList bokelist=(ArrayList)session.getAttribute("bokelist");
  String id2 = request.getParameter("id");
  int id3 = Integer.parseInt(id2);
  method method = new method();
  Boke boke = new Boke();
  boke = method.query(id3);
  String title = boke.getTitle();
  String content = boke.getContent();
  session.setAttribute("title",title);
  session.setAttribute("content",content);
  request.getRequestDispatcher("WEB-INF/jsp/look.jsp").forward(request, response);
 }
 protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  HttpSession session=request.getSession();
  String Sid=request.getParameter("id");
  int id=0;
  if(Sid==null||Sid.equals(""));
  Sid="0";
  try{
   id=Integer.parseInt(Sid);
  }catch(NumberFormatException e)
  {
   id=0;
   e.printStackTrace();
  }
  ArrayList bokelist=(ArrayList)session.getAttribute("bokelist");
  String id2 = request.getParameter("id");
  int id3 = Integer.parseInt(id2);
  method method = new method();
  Boke boke = new Boke();
  boke = method.query(id3);
  String title = boke.getTitle();
  String content = boke.getContent();
  request.setAttribute("id", id3);
  request.setAttribute("title",title);
  request.setAttribute("content",content);
  
  request.getRequestDispatcher("WEB-INF/jsp/edit.jsp").forward(request, response);
 }
 protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  HttpSession session=request.getSession();
  String username=(String)request.getSession().getAttribute("username");
  String password=(String)request.getSession().getAttribute("password");
  ArrayList bokelist=(ArrayList)session.getAttribute("bokelist");
  String id2 = request.getParameter("id");
  int id3 = Integer.parseInt(id2);
  method method = new method();
  Boke boke = new Boke();
  boke = method.query(id3);
  String title = boke.getTitle();
  String content = boke.getContent();
  request.setAttribute("id", id3);
  method.delete(id3);
  
  request.getRequestDispatcher("indexservlet?username="+username+"&password="+password).forward(request, response);
 }
 private void message(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  request.getRequestDispatcher("WEB-INF/jsp/message.jsp").forward(request, response);
 }
}

16.编辑资料的servlet
package sevlet;
import java.io.IOException;
import javax.naming.InterruptedNamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import bean.User;
import moth.method;
public class ZiliaoSevlet extends HttpServlet {
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
  doPost(request, response);
 }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String username = request.getParameter("username");
  String password = (String) request.getSession().getAttribute("password");
  String username1 = (String)request.getSession().getAttribute("username");
  String shiming = request.getParameter("shiming");
  String hangye = request.getParameter("hangye");
  String zhiwei = request.getParameter("zhiwei");
  String xingbie = request.getParameter("xingbie");
  String shengri = request.getParameter("shengri");
  String diqu = request.getParameter("diqu");
  String jianshu = request.getParameter("jianshu");
  
  if(!username.equals(username1))
  {
   JOptionPane.showMessageDialog(null,"昵称不可更改","提示消息",JOptionPane.WARNING_MESSAGE);
   request.getRequestDispatcher("WEB-INF/jsp/message.jsp").forward(request, response);
  }
  else
  {
   User user = new User();
   user.setDiqu(diqu);
   user.setHangye(hangye);
   user.setJianshu(jianshu);
   user.setShengri(shengri);
   user.setShiming(shiming);
   user.setUsername(username);
   user.setXingbie(xingbie);
   user.setZhiwei(zhiwei);
   method md=new method();
   md.update(user,username1);
   request.getRequestDispatcher("indexservlet?username="+username+"&password="+password).forward(request, response);
  }
 }
}

17.查看文章的jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>编辑资料</title>
</head>
<body style="background-image: url('image/4.jpg');text-align: center">
 <form action="${pageContext.request.contextPath }/ZiliaoSevlet">
  <table border="0" cellpadding="0" cellspacing="10" align="center">
   <tr>
    <td align="left">
     <h1>编辑简介</h1><hr><br>
    </td>
   </tr>
   <tr>
    <td align="center">
     <span style="color:red;">*</span>昵称:<input type="text" name="username" value=${user.username } size="20">
     实名:<input type="text" id="shiming" name="shiming" value=${user.shiming } size="20">
    </td>
   </tr>
   <tr>
    <td align="center">
     地区:<input type="text" name="diqu" value=${user.diqu }>
     职位:<input type="text" id="zhiwei" name="zhiwei" value=${user.zhiwei } size="20">
     性别:<input type="radio" name="xingbie" value="男"/>男
      <input type="radio" name="xingbie" value="女"/>女</br>
    </td>
   </tr>
   <tr>
    <td align="center">
     生日:<input type="text" id="shengri" name="shengri" value=${user.shengri } size="50">
     行业:<select name="hangye" id="行业">
       <option>医生</option>
       <option>护士</option>
       <option>程序员</option>
      </select></br>
    </td>
   </tr>
   <tr>
    <td align="center">
     简述:<textarea id="content" name="jianshu" rows="30" cols="100" style="font-size:15px;">${user.jianshu }</textarea>
    </td>
   </tr>
   <tr>
    <td align="center">
     <input type="submit" value="提交" style="background:#7FFFD4;width: 100px;height: 35px;"><br>
    </td>
   </tr>
  </table>
 </form>
</body>
</html>

18.编辑文章的jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>编辑文章</title>
</head>
<body style="background-image: url('image/4.jpg');">
<form action="${pageContext.request.contextPath }/EditServlet">
 <table>
  <tr>
   <td align="left">
    <h2>&nbsp;&nbsp;&nbsp;文章编辑</h2><hr>
   </td>
  </tr>
  <tr>
   <td>
    <input type="hidden" name="id" value="${id }">
   </td>
  </tr>
  <tr>
   <td><h3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;标题:</h3></td>
   <td>
    <input type="text" name="title" value="${title }" style="height:25px;width:1000px;">
   </td>
  </tr>
  <tr>
   <td><h3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;正文:</h3></td>
   <td>
    <textarea rows="5" cols="40" name="content" style="height:450px;width:1000px;">${content } </textarea>
   </td>
  </tr>
  <tr>
   <td colspan="2" align="center">
    <br><input type="submit" value="提交" style="background:#7FFFD4;width: 100px;height: 35px;"/>
   </td>
  </tr>
 </table>
</form>
</body>
</html>

19.编辑文章的servlet
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>编辑文章</title>
</head>
<body style="background-image: url('image/4.jpg');">
<form action="${pageContext.request.contextPath }/EditServlet">
 <table>
  <tr>
   <td align="left">
    <h2>&nbsp;&nbsp;&nbsp;文章编辑</h2><hr>
   </td>
  </tr>
  <tr>
   <td>
    <input type="hidden" name="id" value="${id }">
   </td>
  </tr>
  <tr>
   <td><h3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;标题:</h3></td>
   <td>
    <input type="text" name="title" value="${title }" style="height:25px;width:1000px;">
   </td>
  </tr>
  <tr>
   <td><h3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;正文:</h3></td>
   <td>
    <textarea rows="5" cols="40" name="content" style="height:450px;width:1000px;">${content } </textarea>
   </td>
  </tr>
  <tr>
   <td colspan="2" align="center">
    <br><input type="submit" value="提交" style="background:#7FFFD4;width: 100px;height: 35px;"/>
   </td>
  </tr>
 </table>
</form>
</body>
</html>

20.写文章的servlet
package sevlet;
import java.io.IOException;
import java.sql.Connection;
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 moth.method;
public class XieSevlet extends HttpServlet {
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
 }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doGet(request, response);
  String title=new String(request.getParameter("title").getBytes("ISO-8859-1"),"UTF-8");
  String content=new String(request.getParameter("content").getBytes("ISO-8859-1"),"UTF-8");
  String username = (String) request.getSession().getAttribute("username");
  String password =(String)request.getSession().getAttribute("password");
  
  method md=new method();
  md.save(username,title,content);
  request.getRequestDispatcher("indexservlet?username="+username+"&password="+password).forward(request, response);
 }
}纯jsp实现简单的个人博客纯jsp实现简单的个人博客纯jsp实现简单的个人博客