标签:des style blog http io os ar java for
删除 Delservlet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String id=request.getParameter("id"); LyBean lb =new LyBean(); lb.setId(id); lb.delete(); out.print("<script>window.location=‘admin.jsp‘<script>"); }
登录 Dlservlet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String yhm =request.getParameter("yhm"); if(yhm.equals("admin")) response.sendRedirect("admin.jsp"); else response.sendRedirect("liuyan.jsp"); }
留言 Lyservlet
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("gb2312"); PrintWriter out = response.getWriter(); request.setCharacterEncoding("gb2312"); String lyz = request.getParameter("lyz"); String xx = request.getParameter("xx"); LyBean lb =new LyBean(); lb.setLyz(lyz); lb.setXx(xx); lb.save(); out.print("<script>alert(‘保存成功‘);history.back();</script>"); out.flush(); out.close(); }
LyBean
import java.sql.*; public class LyBean { private String lyz; private String xx; private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getLyz() { return lyz; } public void setLyz(String lyz) { this.lyz = lyz; } public String getXx() { return xx; } public void setXx(String xx) { this.xx = xx; } public void save(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url="jdbc:odbc:db1"; Connection conn = DriverManager.getConnection(url, "", ""); Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.executeUpdate("insert into lyb(lyz,xx) values(‘"+ lyz +"‘,‘"+ xx +"‘)"); stmt.close(); conn.close(); } catch(Exception e){ e.printStackTrace(); } } public void delete(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url="jdbc:odbc:db1"; Connection conn = DriverManager.getConnection(url, "", ""); Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.executeUpdate("delete from lyb where id="+id); stmt.close(); conn.close(); } catch(Exception e){ e.printStackTrace(); } } }
Admin.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%> <% 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 ‘admin.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> <h1>留言信息</h1> <table border="1"> <tr> <td>留言者</td> <td>信息</td> <td>时间</td> <td>操作</td> </tr> <% try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url="jdbc:odbc:db1"; Connection conn = DriverManager.getConnection(url, "", ""); Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery("select * from lyb"); while(rs.next()){ %> <tr> <td><%=rs.getString("lyz")%></td> <td><%=rs.getString("xx")%></td> <td><%=rs.getString("rq")%></td> <td><a href=delservlet?id=<%=rs.getString("id") %>>删除</a></td> </tr> <% } stmt.close(); conn.close(); } catch(Exception e){ e.printStackTrace(); } %> </table> </body> </html>
标签:des style blog http io os ar java for
原文地址:http://www.cnblogs.com/by2lemon/p/4056458.html