Java教程

JavaWeb5.25

本文主要是介绍JavaWeb5.25,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package doservlet;
 
import java.io.IOException;
 
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 dao.MsgDao;
 
/**
 * Servlet implementation class dodelete
 */
@WebServlet("/dodelete")
public class dodelete extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dodelete() {
        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
        doPost(request, response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        int id=Integer.parseInt(request.getParameter("id"));
        MsgDao md=new MsgDao();
        md.delMail(id);
        response.sendRedirect("main.jsp");
    }
 
}


package doservlet;
 
import java.io.IOException;
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 dao.UsersDao;
 
/**
 * Servlet implementation class dologin
 */
@WebServlet("/dologin")
public class dologin extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dologin() {
        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
        doPost(request, response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao ud=new UsersDao();
        if(ud.login(uname, upwd)){
            request.getSession().setAttribute("uname", uname);
            request.getRequestDispatcher("main.jsp").forward(request, response);
        }else{
            response.getWriter().print("登陆失败,跳回登陆页");
            response.setHeader("refresh", "5;url=login.jsp");
        }
    }
 
}


package doservlet;
 
import java.io.IOException;
 
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 dao.UsersDao;
 
/**
 * Servlet implementation class doreg
 */
@WebServlet("/doreg")
public class doreg extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public doreg() {
        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
        doPost(request, response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao ud=new UsersDao();
        ud.reg(uname, upwd);
        request.getRequestDispatcher("login.jsp").forward(request, response);
    }
 
}


package doservlet;
 
import java.io.IOException;
 
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 dao.MsgDao;
import entity.Msg;
 
/**
 * Servlet implementation class dowrite
 */
@WebServlet("/dowrite")
public class dowrite extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dowrite() {
        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
        doPost(request, response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        String uname=(String)request.getSession().getAttribute("uname");
        String addressee=request.getParameter("addressee");
        String title=request.getParameter("title");
        String content=request.getParameter("content");
         
        Msg m=new Msg();
        m.setContent(content);
        m.setSender(uname);
        m.setAddressee(addressee);
        m.setTitle(title);
         
        MsgDao md=new MsgDao();
        md.addMsg(m);
         
        response.getWriter().print("发送成功!!!");
        response.setHeader("refresh", "5;url=main.jsp");
    }
 
}

 

这篇关于JavaWeb5.25的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!