login.jsp <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>Login Page</title> </head> <body> <%@ include file="login_form.jsp" %> </body> </html>
被包含的login_form.jsp <%@ taglib uri="utilities" prefix="util" %> <!--调用自定义标签,引用为util,uri的utilities在web.xml映射了--> <p><font color="#6666CC">请登陆</font></p> <hr> <form name="form1" method="post" action="<%=response.encodeURL("login")%>"><!--login是LoginSevlet通过在web.xml映射了--> <table width="68%" border="0" cellpadding="2" cellspacing="2"> <tr> <td width="33%" align="right">用户名:</td> <td width="67%"> <input type="text" name="userName" value="<util:requestParameter property='userName'/>"></td><!--注意这里用了自定义标签,如果有值就显示--> </tr> <tr> <td align="right">密码:</td> <td><input type="text" name="userPwd" ></td> </tr> <tr align="center"> <td colspan="2"> <input type="submit" name="Submit" value="登陆"> </td> </tr> </table> </form>
LoginServlet.java import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException;
import beans.User; import beans.LoginDB;
public class LoginServlet extends HttpServlet { private LoginDB loginDB;
public void init(ServletConfig config) throws ServletException { super.init(config); loginDB = new LoginDB(); config.getServletContext().setAttribute("loginDB",loginDB); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ String name = request.getParameter("userName"); //从login_form 表单得到值 String pwd = request.getParameter("userPwd"); User user = loginDB.getUser(name,pwd); if(user != null){ //说明存在用户 request.getSession().setAttribute("user",user); //放到session 里面 request.getRequestDispatcher(response.encodeURL("/welcome.jsp")).forward(request,response); //成功转发到welcome.jsp }else{ request.getRequestDispatcher(response.encodeURL("/loginFailed.jsp")).forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ doGet(request,response); } }
web.xml添加 <servlet> <servlet-name>Login</servlet-name> <!--名字--> <servlet-class>LoginServlet</servlet-class> <!--指定类--> </servlet><servlet> <servlet-name>new_account</servlet-name> <servlet-class>NewAccountServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>new_account</servlet-name> <url-pattern>/new_account</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Login</servlet-name> <!--和上面的名字一致--> <url-pattern>/login</url-pattern> <!--映射路径--> </servlet-mapping> <taglib> <taglib-uri>utilities</taglib-uri> <taglib-location>/WEB-INF/tlds/utilities.tld</taglib-location> <!--自定义标签的实际位置--> </taglib>
utilities.tld关键部分 <taglib> <tag> <name>requestParameter</name> <tagclass>tags.GetRequestParameterTag</tagclass> <!--类的位置,如果有包写上--> <info>Simplest example: inserts one line of output</info> <bodycontent>Empty</bodycontent> <attribute> <name>property</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
出处:蓝色理想
责任编辑:帅青蛙
上一页 用MVC架构简单的注册、登录例子 [1] 下一页 用MVC架构简单的注册、登录例子 [3]
◎进入论坛网络编程版块参加讨论
|