.


:




:

































 

 

 

 


2. JSP. .

Java Server Pages (JSP) - J2EE, Web-. JSP Web- , .

, , Java-, HTTP-, HTTP- , HTML. , , / , .

:

  • , ;
  • , ;
  • , ;
  • , ;
  • .

, . javax.servlet.Filter, :

  • void init (FilterConfig config) throws ServletException;
  • void destroy();
  • void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException;

init , , . doFilter . , init , , doFilter , . , , destroy.

package common;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class FilterConnect implements Filter{

private FilterConfig config = null;

private boolean active = false;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

public void init (FilterConfig config) throws ServletException{

this.config = config;

String act = config.getInitParameter("active");

if (act!= null)

active = (act.toUpperCase().equals("TRUE"));

}

 

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public void doFilter (ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException{

if (active){

//

@WebFilter @WebInitParam. web.xml:

<filter>

<filter-name>encodingfilter</filter-name>

<filter-class>by.bsu.sample.filter.EncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingfilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

- . : cookies .
Cookie - , . , cookie - - , . HTTP , . , . cookie , , getCookies() HttpServletRequest:

Cookie cookies[] = request.getCookies();


cookie getName() getValue().

cookie :

 

Cookie theCookie = new Cookie( cookie, cookie);

 

response.setContentType = "test/html";

 

response.addCookie(theCookie);


. , HttpServlet , HTTP. - HTTP , . . , , , getSession():

 

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletRequest {

HttpSession session = request.getSession(true);

 

...


, , . cookie, - . , cookie - (String), (Object). , , putValue getValue. :

 

private static final String SUM_KEY = "sum";

 

Integer count = 100;

 

session.putValue (SUM_KEY, count);

 

...

 

Integer sum = (Integer) session.getAttribute (SUM_KEY);


, HttpSession, Serializable. - , web . setAttribute() getAttribute(). , valueBound() valueUnbound().


, HTTP . , , - .

 

-, .

1. BSAC. web.xml.

2. SessionFilter.

 

 

3. . .

<filter>

<filter-name>SessionFilter</filter-name>

<filter-class>by.bsac.session.SessionFilter</filter-class>

<init-param>

<param-name>ignore- urls </param-name>

<param-value>/BSAC/login,/BSAC/LoginController,/BSAC/error-login.jsp</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>SessionFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

 

: filter-name - ;

filter-class - , ;

init-param , ;

param-value ;

filter-mapping urla ;

url-pattern urla.

.. /* ( url).

 

web.xml :

<?xml version= "1.0" encoding= "UTF-8"?>

<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation= "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version= "3.1" >

<display-name>BSAC</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

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

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<filter>

<filter-name>SessionFilter</filter-name>

<filter-class>by.bsac.session.SessionFilter</filter-class>

<init-param>

<param-name>ignore- urls </param-name>

<param-value>/BSAC/login,/BSAC/LoginController,/BSAC/error-login.jsp</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>SessionFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

<servlet>

<description></description>

<display-name>LoginController</display-name>

<servlet-name>LoginController</servlet-name>

<servlet-class>by.bsac.profile.LoginController</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>LoginController</servlet-name>

<url-pattern>/login</url-pattern>

<url-pattern>/logout</url-pattern>

</servlet-mapping>

</web-app>

 

4. :

package by.bsac.session;

 

import java.io.IOException;

import java.util.ArrayList;

import java.util.StringTokenizer;

 

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import by.bsac.profile.ProfileTools;

 

public class SessionFilter implements Filter {

 

private ArrayList<String> ignoredUrlList;

 

/**

* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)

*/

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;

HttpServletResponse res = (HttpServletResponse) response;

// uri

String requestUri = req.getRequestURI();

 

boolean shouldBeIgnored = isIgnoredUrl(requestUri);

// " url, ?"

// url ,

// login.jsp

if (!shouldBeIgnored &&!ProfileTools.isLoggedIn(req)) {

res.sendRedirect(req.getContextPath() + "/login.jsp");

 

} else {

// pass the request along the filter chain

chain.doFilter(request, response); //

}

}

 

/**

*

* @see Filter#init(FilterConfig)

*/

public void init(FilterConfig fConfig) throws ServletException {

// web.xml ignore-urls

ignoredUrlList = new ArrayList<String>();

// fConfig , init-param

String urls = fConfig.getInitParameter("ignore-urls");

// urlList

StringTokenizer token = new StringTokenizer(urls, ",");

while (token.hasMoreTokens()) {

ignoredUrlList.add(token.nextToken());

}

}

/**

* url url

* @param url - url

*/

private boolean isIgnoredUrl(String url) {

for (String ignoredUrl: getIgnoredUrlList()) {

if (url.startsWith(ignoredUrl)) {

return true;

}

}

return false;

}

 

public ArrayList<String> getIgnoredUrlList() {

return ignoredUrlList;

}

 

public void setIgnoredUrlList(ArrayList<String> urlList) {

this.ignoredUrlList = urlList;

}

 

@Override

public void destroy() {

// TODO Auto-generated method stub

 

}

 

}

 

5. ProfileTools:

package by.bsac.profile;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

 

public class ProfileTools {

public static String SESSION_LOGGEDIN_ATTRIBUTE_NAME = "user";

/* true ,

* . - false*/

public static boolean isLoggedIn(HttpServletRequest request) {

HttpSession session = request.getSession(false);

return session!= null && session.getAttribute(SESSION_LOGGEDIN_ATTRIBUTE_NAME)!= null;

}

}

 

6. login.jsp

1. <%@ page language= "java" contentType= "text/html; charset=UTF-8"

2. pageEncoding= "UTF-8" %>

3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

4. <html>

5. <head>

6. <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" >

7. <title>Login</title>

8. </head>

9. <body>

10. <form action= "login" method= "post" >

11. <input type= "hidden" name= "authAction" value= "login" >

12. <p>Please login by

13. <label for= "email" >Email</label>

14. <input type= "radio" name= "authType" value= "email" id= "email" checked/>

15. or

16. <label for= "userName" >User name</label>

17. <input type= "radio" name= "authType" value= "userName" id= "userName" />

18. <input type= "text" name= "loginValue" >

19. </p>

20. <label for= "psw" >Password</label>

21. <input type= "password" name= "psw" id= "psw" >

22. <p>The characters in a password field are masked (shown as asterisks or circles).</p>

23.

24. <input type= "submit" value= "Submit" />

25. </form>

26. </body>

27. </html>

 

7. LoginController

 

package by.bsac.profile;

 

import java.io.IOException;

 

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

 

/**

* Servlet implementation class LoginController

*/

public class LoginController extends HttpServlet {

private static final long serialVersionUID = 1L;

 

/**

* @see HttpServlet#HttpServlet()

*/

public LoginController() {

super();

// TODO Auto-generated constructor stub

}

 

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.sendRedirect("login.jsp");

}

 

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String authAction = request.getParameter("authAction");

if (authAction.equals("login")){

login(request, response);

} else if (authAction.equals("logout")){

logout(request, response);

} else {

 

}

}

 

 

private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

HttpSession session = request.getSession(false);

if (session!=null) {

session.invalidate();

}

response.sendRedirect("login.jsp");

 

}

 

private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String authTypeParam = request.getParameter("authType");

Authenticator authenticator = new AuthenticatorImpl();

boolean isAuthentificated = false;

String password = request.getParameter("psw");

String authValue = request.getParameter("loginValue");

if (authTypeParam.equals("email")) {

isAuthentificated = authenticator.authenticateByUserEmail(authValue, password);

} else {

isAuthentificated = authenticator.authenticateByUserName(authValue, password);

}

if (isAuthentificated) {

HttpSession session=request.getSession();

session.setAttribute(ProfileTools.SESSION_LOGGEDIN_ATTRIBUTE_NAME, authValue);

response.sendRedirect("home.jsp");

}

else{

response.sendRedirect("error-login.jsp");

}

}

 

}

 

8. Authenticator:

package by.bsac.profile;

 

public interface Authenticator {

public boolean authenticateByUserName(String username, String password);

public boolean authenticateByUserEmail(String email, String password);

}

 

9. AuthenticatorImpl, :

package by.bsac.profile;

 

public class AuthenticatorImpl implements Authenticator {

private String username = "user";

private String password = "password";

private String email = "[email protected]";

 

@Override

public boolean authenticateByUserName(String username, String password) {

if ((getUsername().equalsIgnoreCase(username))

&& (getPassword().equals(password))) {

return true;

} else {

return false;

}

}

@Override

public boolean authenticateByUserEmail(String email, String password) {

if ((getEmail().equalsIgnoreCase(email))

&& (getPassword().equals(password))) {

return true;

} else {

return false;

}

}

 

public String getPassword() {

return password;

}

public String getUsername() {

return username;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this. email = email;

}

 

}

 

10. error-login.jsp

<%@ page language= "java" contentType= "text/html; charset=UTF-8"

pageEncoding= "UTF-8" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" >

<title>Login failed</title>

</head>

<body>

Login failed, please <a href= "/BSAC/login.jsp" >try again</a>.

</body>

</html>

 

11. home.jsp

<%@ page language= "java" contentType= "text/html; charset=UTF-8"

pageEncoding= "UTF-8" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" >

<title>Home</title>

</head>

<body>

<h1>

Hello, <%= session.getAttribute("user") %>

</h1>

<form action= "logout" method= "post" >

<input type= "hidden" name= "authAction" value= "logout" >

<input type= "submit" value= "Logout" />

</form></body>

</html>

 

12. url. login.jsp. / email , home.jsp.

 

 

:

. , .

1. , , , . cookie.

2. . , HTML-, cookie, .

3. , , . ? cookie.

4. cookie , .

5. . cookie, .

6. . cookie, . : , .

7. , , , , . cookie , . : .

8. cookie , , .

9. - . cookie.

10. . , , cookie.

11. cookie.

12. . cookie.

13. HTML-. cookie ( ), .

14. HTML-. cookie.

15. HTML- . , HTML , cookie.

16. . cookie.

17. cookie.

18. (*.by, *.kz . .), , cookie.

19. () cookie.

20. ,

.



<== | ==>
, , | . . .
:


: 2016-11-12; !; : 980 |


:

:

- , 20 40 . - .
==> ...

1691 - | 1632 -


© 2015-2024 lektsii.org - -

: 0.216 .