Add.java
上传用户:sxwtmm
上传日期:2022-08-11
资源大小:2183k
文件大小:3k
- /*
- * Created on 2004-9-24
- *
- * To change the template for this generated file go to
- * Window>Preferences>Java>Code Generation>Code and Comments
- */
- package com.affice;
- import oa.sys.*;
- import oa.sys.Time;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- /**
- ****************************************************
- *类名称: Add<br>
- *类功能: 增加公告信息<br>
- *创建: 白伟明 2004年10月5日<br>
- ****************************************************
- * To change the template for this generated type comment go to
- * Window>Preferences>Java>Code Generation>Code and Comments
- */
- public class Add extends HttpServlet{
- private HttpSession session=null;
- private ResultSet rs=null;
- private Statement stmt=null;
- private String title,content,sqli,sqls;
- private int temp=0,id;
-
- public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
- request.setCharacterEncoding("gb2312");//设定统一编码样式,解决中文显示问题
- response.setContentType("text/html; charset=gb2312");
- Str str=new Str();//获得字符串转化类中的对象
- Db db=new Db();//获得数据库连接对象
- Time time=new Time();//获得显示时间类中的对象
- PrintWriter out=response.getWriter();//获得输出流对象
- session=request.getSession();//返回和客户端关联的Session
- id=Integer.parseInt((String)session.getAttribute("id"));//获得员工登陆的ID号
- title=request.getParameter("title");//接收表单提交过来公告标题
- content=request.getParameter("content");//接收表单提交过来的公告内容
- title=str.inStr(title);//对标题进行字符串的格式转化
- content=str.inStr(content);
- sqli="INSERT INTO affice(title,time,employeeid,content)"+
- " VALUES('"+title+"','"+time.getYMD()+"',"+id+",'"+content+"')";
- sqls="SELECT * FROM affice WHERE time='"+time.getYMD()+"'";
- try {
- stmt=db.getStmtread();
- rs=stmt.executeQuery(sqls);//执行判断今日是否已经发布了公告的SQL语句
- if(!rs.next()){//如果今日还没有发布公告
- db.close();
- stmt=db.getStmt();
- temp=stmt.executeUpdate(sqli);//执行插入的SQL语句
- if(temp>0){
- request.setAttribute("msg","发布成功");//通过"名-值"对的形式将信息保存在Request范围内
- }else{
- request.setAttribute("msg","发布失败");
- }
- }else{
- request.setAttribute("msg","今日已经发布公告,不能再发布");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }finally{
- db.close();
- //处理完客户请求后,将请求转发到相应的JSP页来显示处理结果
- RequestDispatcher dispatcher=request.getRequestDispatcher("add.jsp");
- dispatcher.forward(request,response);
- }
- }
- //doGet()方法的执行效果和doPost()方法一样
- public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
- doPost(request,response);
- }
- }