《留言板系统一利用servlte完成系统改造》由会员分享,可在线阅读,更多相关《留言板系统一利用servlte完成系统改造(10页珍藏版)》请在金锄头文库上搜索。
1、package servlet;import java.io.IOException; import java.io.PrintWriter;import javabean.Op;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
2、/调用过程 init() service() doGet()/doPost public class LoginServlet extends HttpServlet /* Constructor of the object.*/ public LoginServlet() super(); /* Destruction of the servlet. */ public void destroy() super.destroy(); / Just puts “destroy“ string in log / Put your code here /* The doGet method of
3、the servlet. * 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*
4、/ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doPost(request,response);/* The doPost method of the servlet. * This method is called when a form has its tag value method equals to post.* * param request the request send by the clien
5、t 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 /获取 session 对象 Http
6、Session session=request.getSession(); /中文处理 request.setCharacterEncoding(“utf-8“); /获取前一个页面中提交的 name 与 psss String name = request.getParameter(“username“); String pass = request.getParameter(“password“); /连接数据库,执行 sql 语句,获得查找的记录集, /判断提交的用户是否在记录集中,如果在表示登录成功,否则登录失 败,重新登录 /加载 JDBC 驱动 /*Class.forName(“c
7、om.mysql.jdbc.Driver“); /获得 Connection 对象Connection conn= DriverManager.getConnection( “jdbc:mysql:/localhost:3306/lyb“,“root“,“root“); */Op op=new Op();boolean is=false;try is = op.isHas(name, pass); catch (Exception e) / TODO 自动生成的 catch 块 e.printStackTrace(); if (is) /判断是否有记录 session.setAttribute
8、(“username“, name);/session 是内置对象 response.sendRedirect(“main.jsp“); else /重定向到 login.jsp response.sendRedirect(“login.jsp?ok=no“); /* Initialization of the servlet. * throws ServletException if an error occurs*/ public void init() throws ServletException / Put your code here package servlet;import
9、java.io.IOException;import java.io.PrintWriter;import javabean.Op;import javabean.usertable;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class RegisterServlet extends Http
10、Servlet /* Constructor of the object.*/public RegisterServlet() super();/* Destruction of the servlet. */public void destroy() super.destroy(); / Just puts “destroy“ string in log/ Put your code here/* The doGet method of the servlet. * This method is called when a form has its tag value method equa
11、ls 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)th
12、rows ServletException, IOException doPost(request,response);/* The doPost method of the servlet. * 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* t
13、hrows ServletException if an error occurred* throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException request.setCharacterEncoding(“utf-8“);String name=request.getParameter(“username“);String pass=reques
14、t.getParameter(“password“);/创建一个Usertable类的对象usertable user=new usertable();/设置值user.setName(name);user.setPass(pass);Op op=new Op();int n = 0;try n = op.regist(user); catch (Exception e) / TODO 自动生成的 catch 块e.printStackTrace();if(n0)response.sendRedirect(“success.jsp“);else response.sendRedirect(“r
15、egister.jsp“);/* Initialization of the servlet. * throws ServletException if an error occurs*/public void init() throws ServletException / Put your code herepackage servlet;import java.io.IOException; import java.io.PrintWriter; import java.util.*;import javabean.Op;import javax.servlet.ServletExcep
16、tion; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class ChangeServlet extends HttpServlet /* Constructor of the object.*/ public ChangeServlet() super(); /* Destruction of the servlet. */ public void destroy() super.destroy(); / Just puts “destroy“ string in log / Put yo