What is the best way to create a Java servlet or JSP that does not necessarily include content depending on URL parameters

I want to create a JSP page or servlet that will work in two ways.

The user views the page of his own profile:

HTTP // something.com / profile /

Or they visit their friends page:

http://something.com/profile/FriendsName

They can also visit their own page using an explicit URL, for example: http://something.com/profile/YourName

I have a servlet mapping setup, as shown below, to map any requests / profile to my JSP that will handle this request.

  <servlet>
          <servlet-name>Profile</servlet-name>
          <jsp-file>/profile.jsp</jsp-file>
  </servlet>

  <servlet-mapping>
    <servlet-name>Profile</servlet-name>
    <url-pattern>/profile</url-pattern>
  </servlet-mapping>

, , URL- HTTPServletRequest /profile/.

  <filter>
    <filter-name>profile-filter</filter-name>
    <filter-class>ProfileFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>profile-filter</filter-name>
    <url-pattern>/profile*</url-pattern>
  </filter-mapping>

HttpServletRequest, JSP, , , ?

bean ServletFilter, JSP jspBean :

<jsp:useBean id="profileInfo" scope="page" class="ProfileInfo" /> 
+3
2

, . URL-, url, web.xml. HttpServletRequest , , getPathInfo(). :

<%
   String path = request.getPathInfo();
   if (path == null || "".equalsIgnoreCase(path)) {
      // The path was empty, display the current user profile
   } else {
      // Display the named profile
   }
%>

beans, , .

+1

, , jsp , :

<%
  String parameter = request.getParameter("parameter");
  String attribute = request.getAttribute("attribute");

%>

URL- .., - .

jsp (, , jsp).

, .

0

Source: https://habr.com/ru/post/1707896/


All Articles