⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 b0073b8e9988001d1242e3c5478cf144

📁 一个相对比较简单的留言簿!功能还比较齐全!
💻
字号:
package com.pure35.servlet;

import java.io.IOException;
import java.sql.Date;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pure35.bean.SqlDate;
import com.pure35.database.MessageDao;
import com.pure35.vo.MessageVo;


public class AddMessage extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		this.doPost(request, response);
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//设置编码格式
		request.setCharacterEncoding("GBK");	
		//取得登陆用户信息
		
		HttpSession session=request.getSession();
		String  user=(String) session.getAttribute("UserName");
		if(user==null){
			
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
		//
		//得到用户提交留言信息
		String subject=request.getParameter("subject");
		String content=request.getParameter("content");
		//获得提交时间
		Date mdate=SqlDate.getSqlDate();
		
		//把信息封装到MessageVo
		MessageVo message=new MessageVo();
		message.setContent(content);
		message.setMdate(mdate);
		message.setSubject(subject);
		message.setUsername(user);
		
		//把信息写入数据库
		MessageDao mesdao=new MessageDao();
		boolean result=mesdao.addMessage(message);
		//成功后跳转到留言显示页面
		if(result)
			request.getRequestDispatcher("/ShowMessage").forward(request, response);
		//失败后提示信息
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -