實訓第四天 p2pdemo登錄功能

1.

2.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path的解釋說明

 

這其實就是 獲得應用的根url,比如說你的應用的根路徑是 http://localhost:8080,那麼你列出的代碼就是爲basePath賦值爲 http://localhost:8080。
具體點:
       1、request.getScheme() 返回協議的名稱   http,和後面的"://" 拼起來就成了 http://
       2、request.getServerName() 這是獲取你的服務器的名稱,如果你的應用部署在本機那麼其就返回localhost或者127.0.0.1 ,這2個是等價的
       3、request.getServerPort()  是你應用使用的端口,比如8080或者80  等等

1.

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
     <%
       response.sendRedirect(request.getContextPath()+"/user/showLogin.do");
     %>
  </body>
</html>

response.sendRedirect 的功能是地址重定向(頁面跳轉)

 

這邊重定向是/user/showLogin.do,然後去Struts-config.xml文件裏找到<action path="/user/showLogin" forward="/WEB-INF/jsp/login.jsp"/>然後跳轉到login.jsp頁面

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="h"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
	</head>
	<body>
		<h1>用戶登錄<h:errors/></h1>
		<hr/>
		
		<form action="${pageContext.request.contextPath}/user/login.do" method="post">
			用戶名:<input type="text" name="username"/><br/>
			密碼:<input type="password" name="password"/><br/>
			<input type="submit" value="登錄"/>
		</form>
		
	</body>
</html>
<action path="/user/login" 
			type="action.UserAction" 
			parameter="login" 
			input="/WEB-INF/jsp/login.jsp">
			<exception key="exception.userNotFound" type="exception.UserNotFoundException"></exception>
			<forward name="success" path="/user/selectAll.do" redirect="true"/>
		</action>

action的name決定調用時的名稱;class爲實現類名;result的值爲action執行完成後轉向何頁面。如果沒有爲result指定name名稱,默認值爲success

<exception>

 path:出現異常跳轉的界面

key:異常信息的鍵

type:所要處理的異常的

UserAction.java

	//登錄
	public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		UserService userService=(UserService) ObjectFactory.getObject("userService");
		userService.login(username, password);
		return mapping.findForward("success");
	}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章