• servlet


    xml:

    <servlet>
    <servlet-name>initparam</servlet-name><!-- 与servlet-mapping相对应 -->
    <servlet-class>
    com.servlet.InitParamServlet
    </servlet-class>
    <init-param><!-- 配置参数 -->
    <param-name>ref</param-name>
    <param-value>www.test.cn</param-value>
    </init-param>
    </servlet>
    <servlet-mapping><!-- 映射路径 -->
    <servlet-name>initparam</servlet-name><!-- 与servlet相对应 -->
    <url-pattern>/InitParamServlet</url-pattern><!-- 页面的映射路径 -->
    </servlet-mapping>

    class文件:

    package com.servlet;

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

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

    import com.factory.DAOFactory;
    import com.vo.User;

    public class LoginServlet extends HttpServlet {

    /**
    * 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 {
    String path = "login.jsp";
    String userid = request.getParameter("userid"); // 接收userid内容
    String userpass = request.getParameter("userpass"); //接收userpass内容

    List<String> info = new ArrayList<String>(); // 保存所有返回信息
    if (userid == null || "".equals(userid)) {//用户名为null
    info.add("用户id不能为空!"); // 增加错误信息
    }
    if (userpass == null || "".equals(userpass)) {
    info.add("密码不能为空!"); // 增加错误信息
    }

    if (info.size() == 0) { // 用户名和密码验证通过
    User user = new User(); // 实例化VO
    user.setUserid(userid);
    user.setPassword(userpass);

    try {
    if (DAOFactory.getIUserDAOInstance().findLogin(user)) {//验证通过
    info.add("用户登录成功,欢迎" + user.getName() + "光临!");
    } else {
    info.add("用户登录失败,错误的用户名或密码!");
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    request.setAttribute("info", info);
    request.getRequestDispatcher(path).forward(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 {
    this.doGet(request, response);
    }

    }

  • 相关阅读:
    MyBatis的动态SQL语句这么厉害的!
    连接数据库,使用c3p0技术连接MySQL数据库
    Servlet 常见的乱码解决方案
    超级签具体实现
    Xcode报错You don’t have permission.
    SpringBoot+Mybatis整合实例
    恢复mysql数据库误删数据
    日期(date)运用座谈会
    程序猿日记--学习怎样学习
    服务器数据库密码忘记
  • 原文地址:https://www.cnblogs.com/xingmeng/p/3022324.html
Copyright © 2020-2023  润新知