Java开发网 |
注册 |
登录 |
帮助 |
搜索 |
排行榜 |
发帖统计
|
您没有登录 |
» Java开发网 » Design Pattern & UML
打印话题 寄给朋友 订阅主题 |
作者 | 2 [Re:prettyxx] |
prettyxx
发贴: 75 积分: 42 |
于 2003-12-16 13:29
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",loginD; } 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> 我的Blog:阿泠的自省札记 ========心有多大,舞台就有多大======== |
话题树型展开 |
人气 | 标题 | 作者 | 字数 | 发贴时间 |
7466 | [转帖]用MVC架构简单的注册、登录例子 | prettyxx | 2237 | 2003-12-16 13:28 |
6278 | 2 | prettyxx | 3282 | 2003-12-16 13:29 |
6429 | 3 | prettyxx | 1468 | 2003-12-16 13:30 |
6315 | 4 | prettyxx | 1756 | 2003-12-16 13:31 |
6420 | Re:[转帖]用MVC架构简单的注册、登录例子 | ccw | 4 | 2004-01-04 09:52 |
已读帖子 新的帖子 被删除的帖子 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |