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" %> <!--調(diào)用自定義標(biāo)簽,引用為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><!--注意這里用了自定義標(biāo)簽,如果有值就顯示--> </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); //成功轉(zhuǎn)發(fā)到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> <!--自定義標(biāo)簽的實(shí)際位置--> </taglib>
utilities.tld關(guān)鍵部分 <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>
出處:藍(lán)色理想
責(zé)任編輯:帥青蛙
上一頁 用MVC架構(gòu)簡單的注冊、登錄例子 [1] 下一頁 用MVC架構(gòu)簡單的注冊、登錄例子 [3]
◎進(jìn)入論壇網(wǎng)絡(luò)編程版塊參加討論
|