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