I am developing a web application in Java that takes a string containing a website URL, parses the website HTML to gather information, then uses that information to query a postgresql database. This is already written using Java, HTML, JS and CSS.
In my HTML I have a text input box where the user can paste a URL and submit it using a button. What I would like to do, is get this URL as the String I mentioned above in my Java code, versus hard coding it myself. Eventually, after parsing that URL HTML file and running whatever queries I need on my database, I will return the query results back to the browser for the user to see.
I understand that JavaScript runs in the browser while my Java source code is server side at different times. I've gathered that a possible solution is to submit a HTTPServletRequest in my Java source code that communicates with my JavaScript; however I am unsure of the right steps to accomplish this. XmlHTTPRequest is something else I've seen being used.
Edit - After further reading I am deciding between programming a Java servlet, or a JSP to handle this. I am leaning towards a servlet as I am more familiar with Java than HTML, and will be using more logic (HTML parsing, RDBMS querying using jdbc, returning data).
Does this seem to be the correct decision?
I hope I worded this clearly and that this is a valid question! Thank you!
UPDATE/EDIT
Here is my code I've done so far after thinking about Mois44's answer. I am unsure what to put for the URL in my xmlHttp.send() request. In the browser, there is a text box, and submit button for the user as I said.
Error: url.html:91 POST http://localhost:8080/myapplication/GetURL?url=http://mywebsite.com/category/123986/webpage/ 404 (Not Found)
This is the project structure for these files:
src/main/
|
|----java/
| |
| |----path/to/java/servlet/myServlet.java
|
|----webapp/
|
|----META-INF/
| |----web.xml
|
|----pages/
|----url.html
|
index.html
web.xml:
<servlet>
<servlet-name>GetURL</servlet-name>
<servlet-class>path.to.java.servlet.myServlet</servlet-class>
<init-param>
<param-name>url</param-name>
<param-value>www.testurl.com</param-value> // don't I set this in my url.html from user?
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet/*</url-pattern> // not sure about this...
</servlet-mapping>
url.html:
<div class="url-input">
<input type="text" id="txtUrl" class="text-box" value="Paste recipe website URL here" align="middle">
<button onclick="urlRequest()" id="myid" value="myvalue">Go!</button>
<script>
function getURL(xmlHttp) {
document.getElementById("myid").value = xmlHttp.responseText;
return document.getElementById("txtUrl").value
}
</script>
<script>
function urlRequest() {
var xmlHttp = new XMLHttpRequest();
var url = getURL(xmlHttp);
xmlHttp.open('POST', 'http://localhost:8080/myapplication/GetURL?url='+url, true);
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState = 4 && xmlHttp.status == 200) {
alert(xmlHttp.responseText);
}
};
xmlHttp.send(url);
}
</script>
</div>
myServlet.java:
public class Servlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
handleRequest(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
handleRequest(request, response);
}
protected void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String p = request.getParameter("url");
System.out.println("test");
System.out.println(p);
}
}