• JSP 08


    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Insert title here</title>
    <style type="text/css">
        #div{ 
            color: red;
        }
    </style>
    </head>
    <body>
        <form action="writeServlet">
            标题:<input type="text" name="title"><br>
            内容:<input type="text" name="content"><br>
            发送至:<input type="text" name="sendto"><br>
            <input type="submit" value="确认发送"> 
        </form>
        <div id="div">
            <%=request.getSession().getAttribute("xx")==null?"":request.getSession().getAttribute("xx")%>
        </div>
        <%request.getSession().removeAttribute("xx"); %>
    </body>
    </html>
    package servlet;
    
    import java.io.IOException;
    import java.util.Date;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import Utils.JDBCUtils;
    import domain.Email;
    
    /**
     * Servlet implementation class writeServlet
     */
    @WebServlet("/writeServlet")
    public class writeServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public writeServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            String sendto = request.getParameter("sendto");
            Email email = new Email();
            email.setTitle(title);
            email.setContent(content);
            email.setSendto(sendto);
            email.setSend_date(new Date());
            JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
            String sql="insert into email value (null,?,?,?,?,?)";
            template.update(sql,email.getTitle(),email.getContent(),email.getSendto(),"1",email.getSend_date());
            request.getSession().setAttribute("xx", "发送成功");
            request.getRequestDispatcher("/write_email.jsp").forward(request, response);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
  • 相关阅读:
    暑假第一周总结
    洛谷P3378 【模板】堆 题解 堆(Heap)入门题
    洛谷P2170 选学霸 题解 并查集+01背包
    洛谷P1433 吃奶酪 题解 状态压缩DP
    洛谷P2835 刻录光盘 题解 点的度数+并查集
    洛谷P1991 无线通讯网 题解 并查集+二分答案
    洛谷P4185 [USACO18JAN]MooTube G 题解 并查集
    洛谷P4145 上帝造题的七分钟2 / 花神游历各国 题解 线段树+懒惰标记
    洛谷P2658 汽车拉力比赛 题解 二分答案+搜索
    洛谷P1546 最短网络 Agri-Net 题解 最小生成树/Prim算法
  • 原文地址:https://www.cnblogs.com/yanzhen108/p/12875424.html
Copyright © 2020-2023  润新知