登录后主页面
示例代码如下所示:
1)注册代码:package servlet;
import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import bean.userBean;
import dao.user;
31
public class ZhuceServlet extends HttpServlet {
/**
* Constructor of the object. */
public ZhuceServlet() { super(); }
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts \ // Put your code here }
/**
* The doGet method of 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 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { }
/**
* 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 * @throws ServletException if an error occurred * @throws IOException if an error occurred */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
32
String name=request.getParameter(\ String pwd=request.getParameter(\ user test=new user(); try {
userBean user=test.check(name); if(user==null){
userBean user1=new userBean(name,pwd); test.register(user1); response.sendRedirect(\ }else{
response.sendRedirect(\ }
} catch (Exception e) {
// TODO Auto-generated catch block e.printStackTrace(); }
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs */
public void init() throws ServletException { // Put your code here } }
2)登陆代码: package servlet;
import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.List;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import dao.library; import dao.user;
import bean.libraryBean;
33
import bean.userBean;
public class LoginServlet extends HttpServlet {
/** * */
private static final long serialVersionUID = 1L;
/**
* Constructor of the object. */
public LoginServlet() { super(); }
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts \ // Put your code here }
/**
* The doGet method of 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 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(\ String name=request.getParameter(\ String pwd=request.getParameter(\ //userBean user = new userBean(name,pwd); user test=new user(); try {
userBean user = test.check(name); if (user!=null){
34
if (pwd.equals(user.getPWD())){
library bookinfo=new library();//Dao涓?殑library List
// response.sendRedirect(request.getContextPath() + \
request.getRequestDispatcher(\ }else{
response.sendRedirect(\ } }else{
response.sendRedirect(\ } } catch (Exception e) {
// TODO Auto-generated catch block e.printStackTrace(); } }
/**
* 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 * @throws ServletException if an error occurred * @throws IOException if an error occurred */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(\ doGet(request,response); }
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs */
public void init() throws ServletException { // Put your code here } }
35