Issue
I am using JSP with an Apache Tomcat server for a web project.
I do not understand .do
files or how they are used, but I see them come up in answers describing how to do things I need to do.
My understanding thusfar is this:
- The Struts framework uses
.do
files, so many people think of these in relation to one another .do
files are described in the Java Servlet Specification and are thus not specific to Struts- At least one major use case of
.do
files (which I'm interested in) is for easily passing data between HTML/JS forms and Java/JSP.
If I create a form such as the one in the first answer I linked:
<form action="MyServlet.do" method="post">
<input type="text" name="nameThree" value="Enter a Name" onClick="if(this.value == 'Enter a Name'){this.value = '';}" />
<input type="submit">
</form>
It will throw a 404 error, unless I create the .do
file it mentions. Once that .do
file is created the code runs, but does not appear to do anything because of some missing piece.
I'm not sure if the .do
file is supposed to be empty or if I am supposed to put code in it, etc, etc.
Thus my question is: Outside of the context of the Struts framework, how are .do files used in JSP for passing data between Java and front end web forms?
Solution
I realize this can be a bit tricky to explain, so I'll do what I can.
".do" extensions (in the URL request) are most commonly found in struts 1 based web applications. That said, the way it works is that a webapp is configured to route http request with .do extensions to a strut servlet (usually in web.xml). This is how struts do its thing (map http requests to forms, identifies post vs get request, forwards to action classes, etc).
In a non-struts app, your http request need routing to a resource to be served. You can do this with a servlet. Alternatively, you can just return your static file (if you write your own file.do), but Tomcat won't be recognizing that file extension as a JSP that needs to be translated and compiled (hence Tomcat gives you a 404).
Likely you still need to configure your server (tomcat) to serve HTML pages. JSP is one of the many in which you can create dynamic HTML on the server. You can do a lot of things (that in today's standards are consider bad practices such as making db connections, making rest calls, etc). At the end, you can write Java in your JSP.
If you are building a simple webapp, I recommend using a different easier framework like Spring Boot to make it easier. If you absolutely want to do JSP, then make sure you http request always fetch .jsp files. If you need special extension, you'll need to map those to a servlet and write/configure it in web.xml.
Hope this helps!
Answered By - Alberto
Answer Checked By - Mary Flores (JavaFixing Volunteer)