4月7日 JavaWeb 周六

<%@ 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>列表页面</h2>
        <table border="1px">
        <tr>
            <th>编号</th>
            <th>新闻注册号</th>
            <th>新闻名称</th>
            <th>新闻内容</th>
            <th>新闻发布日期</th>
            <th>操作</th>
        </tr>
        <c:forEach var="li" items="${list00}">
            <tr>
                <td>${li.id}</td>
                <td>${li.numbera}</td>
                <td>${li.title}</td>
                <td>${li.content}</td>
                <td>${li.datea}</td>
                <td><a href="/code03/upd.jsp?m=${li.id}">修改</a>|<a href="/code03/del?w=${li.id}">删除</a></td>
            </tr>
        </c:forEach>
        </table>
    </center>
  </body>
</html>
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.mysql.jdbc.Driver;
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");
        //创建集合
        List list = new ArrayList();
        //JDBC
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //连接
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/moon", "root", "root");
            //编写sql语句
            String sql = "select * from t_news;";
            //预处理
            PreparedStatement pst = con.prepareStatement(sql);
            //获取结果集
            ResultSet rs = pst.executeQuery();

            while(rs.next()){
                String ids = rs.getString(1);
                String numberas = rs.getString(2);
                String titles = rs.getString(3);
                String contents = rs.getString(4);
                String dateas = rs.getString(5);
                //new对象
                Elep el = new Elep(ids,numberas,titles,contents,dateas);
                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);
    }

}
<%@ 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 'upd.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="/code03/upd" method="post">
            编号:<input type="text" name="bian"/><br>
            新闻注册号:<input type="text" name="hao"/><br>
            新闻名称:<input type="text" name="ming"/><br>
            新闻内容:<input type="text" name="rong"/><br>
            新闻发布日期:<input type="text" name="qi"/><br>
            <input type="submit" value="修改">
        </form>
    </center>
  </body>
</html>
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 UpdServlet 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 numberau = request.getParameter("hao");
        String titleu = request.getParameter("ming");
        String contentu = request.getParameter("rong");
        String dateau = request.getParameter("qi");

        String idu = request.getParameter("bian");
        //JDBC
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //连接
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/moon", "root", "root");
            //编写sql语句
            String sql = "update t_news set numbera=?,title=?,content=?,datea=? where id=?;";
            //预处理
            PreparedStatement pst = con.prepareStatement(sql);
            //修改
            pst.setString(1, numberau);
            pst.setString(2, titleu);
            pst.setString(3, contentu);
            pst.setString(4, dateau);
            pst.setString(5, idu);
            //获取改变行数
            int x = pst.executeUpdate();


            if(x>0){
                response.sendRedirect("/code03/show");
            }


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}
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 javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DelServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        String w = request.getParameter("w");
        //JDBC
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //连接
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/moon", "root", "root");
            //编写sql语句
            String sql = "delete from t_news where id=?;";
            //预处理
            PreparedStatement pst = con.prepareStatement(sql);

            pst.setString(1, w);
            //获取改变行数
            int x = pst.executeUpdate();


            if(x>0){
                response.sendRedirect("/code03/show");
            }


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}
package com.www.bean;

public class Elep {
    //属性
    String id;
    String numbera;
    String title;
    String content;
    String datea;
    //构造方法
    public Elep(String id, String numbera, String title, String content,
            String datea) {
        super();
        this.id = id;
        this.numbera = numbera;
        this.title = title;
        this.content = content;
        this.datea = datea;
    }
    //get和set
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getNumbera() {
        return numbera;
    }
    public void setNumbera(String numbera) {
        this.numbera = numbera;
    }
    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;
    }
    public String getDatea() {
        return datea;
    }
    public void setDatea(String datea) {
        this.datea = datea;
    }




}

请告诉我谁不是奴隶。有的人是“色欲”的奴隶,有的人是“贪婪”的奴隶,有的人是“野心”的奴隶,所有的人又都是“恐惧”的奴隶
——塞内加

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值