码迷,mamicode.com
首页 > Web开发 > 详细

JSP 08

时间:2020-05-12 13:23:49      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:cep   imp   ndt   super   prot   http   att   protected   isp   

技术图片

技术图片

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

}

 

JSP 08

标签:cep   imp   ndt   super   prot   http   att   protected   isp   

原文地址:https://www.cnblogs.com/yanzhen108/p/12875424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!