开发者

Using Dropdown box in JSP code

开发者 https://www.devze.com 2023-01-13 12:52 出处:网络
<html> <body> <form action=\"Test1.jsp\" method=\"post\"> <select name=\"source\" onchange=\"\">
<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="sourc开发者_开发问答e1" 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!


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消