studentPDY

JSP第八次作业

<%@ 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);
    }

}

 

 

 

posted on 2020-05-12 16:22  studentPDY  阅读(91)  评论(0编辑  收藏  举报

导航