zoukankan      html  css  js  c++  java
  • 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);
    }

    }

  • 相关阅读:
    Linux
    Linux -- 文件统计常用命令
    再论最小二乘
    再论EM算法的收敛性和K-Means的收敛性
    【MySQL】优化—工欲善其事,必先利其器之EXPLAIN
    【Linux】浅谈段页式内存管理
    【Linux】之系统工具top
    【Linux】之shell特殊变量整理
    【Linux】系统 之 RAID
    【MySQL】binlog缓存的问题和性能
  • 原文地址:https://www.cnblogs.com/xingmeng/p/3022324.html
Copyright © 2011-2022 走看看