0
<html>
<body>
<form action="Test1.jsp" method="post">

<select name="source" onchange="">
<option value="rss">RSS LINK</option>
<option value="other">OTHER LINK</option>
</select>       

Enter URL to be added   <input type="text" name="url" size=50>

Enter the Source Name of the URL<t><input type="text" name="source1" size=50>

<input type="Submit" name="submit1" value="Add URL in DB">
</form>   
</body>
</html>

The above code is stored in Addurl1.jsp file which calls the other jsp file named Test1.jsp. The code under Test1.jsp is as follows

<%@ page import="myfirst.*" %>
<%
 String url1=request.getParameter("url");
 String source1=request.getParameter("source1");
 myfirst.SearchLink p=new myfirst.SearchLink();
 String result=p.addURL(url1,source1);
 out.println(result);
 System.out.println(result);
%>

Test1.jsp calls addURL(String, String) function of SearchLink.java program. In the dropdownbox of Addurl1.jsp program, if the user selects RSS link, the addURL() method must be called. If the user selects OTHER LINK, there is another method named addURL1() in the same java program which must be called.

Please let me know how the above codes can be modified inorder to achieve my task.

Thanks in advance!

1 Answer 1

2

At first it is better to change Addurl1.jsp into a servlet and implement the doPost method. Jsp files are supposed to only contain the presentation layer and no Java code. Java code should go in servlets (or controllers if you are using an MVC framework).

What you are asking can easily be achieved with an if statement:

final String RSS_LINK = "rss";
final String OTHER_LINK = "other";

String url1=request.getParameter("source");
String result="";
if (url1 != null && url1.equals(RSS_LINK)) {
    result=p.addURL(url1,source1);
}
else if (url1 != null && url1.equals(OTHER_LINK)) {
    result=p.addURL1(url1,source1);
}
Sign up to request clarification or add additional context in comments.

4 Comments

url1.equals(RSS_LINK), url1.equals(OTHER_LINK) how it is associated with the dropdown box of the first program. I cannot understand. Please advise.
yeah.. thanks for advising on using servlet and implementing doPost method. I will start learning and implementing that soon. Btw, if u can give me a good resource to learn, It would be of great use. Thanks for your response.
See my edited answer. When you post the form, the variable source will contain either 'rss' or 'other', that is one of the values in the select element (I've now noticed you have two elements named 'source' - this is a mistake). There are many questions here in SO discussing resources for learning JSP and servlets.
Yeah i will find those resources. Thanks kgiannakakis

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.