Hi I am designing a web application. the structure is like this.


request for product -> controller.jsp ( based on URL forward the request) -> products.jsp

Here request can come from any of the pages and products.jsp refer to different jsps for different product. In general, there will be so many product jsps and one controller.jsp for controlling the requests.

Now, the URL for home.jsp will be .. http://myweb.com/
From home if I want to go to Product1 the URL will be "http://myweb.com/controller.jsp?path=products/product1". Here everytime I need to show the controller.jsp on URL as it is the main controller for navigation.

Is there any way to go Product1.jsp via controller.jsp with URL something like, http://myweb.com/product1..

Please let me know with the steps, what should I do in order to make this happen.

REALLY REALLY APPRECIATE IN ADVANCE.

Recommended Answers

All 3 Replies

Am I correct to say you using GET method instead of POST method?

Am I correct to say you using GET method instead of POST method?

I sm using the default method. i don't have form or anything.
Here is a peace of code..

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> Test</title>
</head>
<body>
<p align="center"> <a href="controller.jsp?path=/data/home/GKHome" >Go To Home</a> </p>
</body>
</html>

and in controller.jsp I am getting the URL and populating xml for home.

<%@page import="com.seo.KAXMLBuilder"%>

<%@ page import="org.jdom.Element,org.jdom.Document,org.jdom.input.SAXBuilder,java.util.List,java.util.Iterator"
%>
<%

	String path = request.getParameter("path");
	StringBuffer URL = request.getRequestURL(); //  [url]http://localhost:8080/seo_prototypecrew/contoller.jsp[/url]
	String urlFromBrowser = URL.toString();
	urlFromBrowser = urlFromBrowser.substring(0,urlFromBrowser.lastIndexOf("/"));
	if(null != path && !path.equals( "" )){
		path=urlFromBrowser.concat(path.concat(".xml"));
		//System.out.print(path);
	}
	KAXMLBuilder builder = new KAXMLBuilder();
	builder = builder.uploadXML(path);
%>
<% if (builder.isRootDataNodeExists()){ %>
<html>
<head>
<title><%=builder.getTitleNodeText()%></title>
</head>
<body>
<div="menu">
<%
              	for (int i = 0; i < builder.getAHrefNodeIds().size(); i++) {
              		String aHrefIdLocal = (String) builder.getAHrefNodeIds().get(i);
              		String aHrefTextLocal = (String) builder.getAHrefNodeTxts().get(i);
              %>
<button onclick="window.location.href='<%=aHrefIdLocal%>'"><%=aHrefTextLocal%>
</button>
<%
       	}
       %>
</div>
</body>
</html>

Try using UrlRewriteFilter for serious uses or using a servlet for controller and making appropriate entry in the web.xml file.
With URLRewrite it would be something along the lines of:

<rule>
    <from>^/products/([0-9]+)$</from>
    <to>/products/index.jsp?product_id=$1</to>
    </rule>

For e.g. suppose you have a controller which handles the application flow, the web.xml would look like something:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">
   
  <description>Testy-web-app</description>
  <display-name>Servlet and JSP Examples</display-name>

  <servlet>
    <servlet-name>Controller</servlet-name>
    <servlet-class>home.projects.testy.servlet.ControllerServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern>/products/*</url-pattern>
  </servlet-mapping>
  
</web-app>

You home page would look something like:

<html>
  <head>
    <title>Product database</title>
  </head>
  <body>
    <div>
      Please choose a product:
      <ul>
        <li><a href="./products/product1">Product 1</a></li>
        <li><a href="./products/product2">Product 2</a></li>
      </ul>
    </div>
  </body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.