博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
servlet
阅读量:5825 次
发布时间:2019-06-18

本文共 2579 字,大约阅读时间需要 8 分钟。

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);
}

}

转载地址:http://jfsdx.baihongyu.com/

你可能感兴趣的文章
如何基于Redis Replication设计并实现Redis-replicator?
查看>>
浮点数内存如何存储的
查看>>
贪吃蛇
查看>>
EventSystem
查看>>
用WINSOCK API实现同步非阻塞方式的网络通讯
查看>>
玩一玩博客,嘿嘿
查看>>
P1352 没有上司的舞会
查看>>
ios11文件夹
查看>>
【HLOJ 559】好朋友的题
查看>>
Electric Fence(皮克定理)
查看>>
nvl 在mysql中如何处理
查看>>
MyEclipse 快捷键
查看>>
快速傅里叶变换FFT
查看>>
大数据常用基本算法
查看>>
JavaScript学习笔记(十三)——生成器(generator)
查看>>
hibernate保存失败
查看>>
MySQL增量订阅&消费组件Canal POC
查看>>
Sqlite多线程
查看>>
数据结构-时间复杂度
查看>>
对象与字符串相互转换
查看>>