Links in server examples

I was checking some of your tutorial server examples and I notice you do something like this (from Server->JSP

<!DOCTYPE html>
<html>
	<head>
		<title>My Home Page</title>
	</head>
	<body>
		<h1>Welcome to my web page!</h1>
		<p>Click <a href="http://localhost:8080/MyWebApp/current-time">here</a> to see the current time.</p>
	</body>
</html>

You create a reference to your localhost. If you deploy, you would need to change all these lines to relative path, right? I was wondering why you wrote it this way and don’t stick to relative paths. I can see it is clear for a beginner but I would think relative paths would be the right approach at the end.

Kf

Mostly the reason I took this approach was because the tutorials use webapps underneath directories: stuff like MyWebApp and HelloWebApp which can be accessed via URLs like http://localhost:8080/MyWebApp and http://localhost:8080/HelloWorldWebApp. Using relative URLs with these is a bit annoying, so I opted for the full URL just to make things explicit.

But you’re right- in the end, relative URLs are the way to go. In a “real” project you probably only have one web app at the top level which can be accessed via the URL http://localhost:8080, which makes relative URLs much easier.

In hindsight I should probably have just used single top-level web apps from the beginning. I’m always happy to accept pull requests for this kind of thing! :stuck_out_tongue:

Single top-level, do you mean they are located in the same under the same url pattern as defined in the <servlet-mapping> in the web.xml file? I think your approach also demonstrates how to hide jsp from the user.

So in the final product, could I replace <a href="http://localhost:8080/MyWebApp/current-time"> with <a href="/MyWebApp/current-time"> ?

Kf

Not exactly. I mean that basically I should have used the root web app for everything and only focused on one web app at a time.

This is explained a bit here:

Then the URL would be:

<a href="/current-time"> 

This matches how things work when you deploy it to a “real” server, and it simplifies things quite a bit.

1 Like