Create URL based on user input - java

I am very new to Java and will like to learn it by applying in my work.
I will like to generate a link based on user input via drop down list.
https://example.com/"input1"/testing_"input2"_xx.html
Input 1 drop down list
Input 2 is date
How do I concatenate input 1 and 2 into the URL?

You have to get the values of the input fields:
function generateURL()
{
var part1 = document.getElementById('part1').value;
var part2 = document.getElementById('part2').value;
var url = "https://example.com/"+part1+"/"+part2+".html";
document.getElementById('result').innerHTML = url;
}
Part 1 of URL:<br/>
<select id="part1">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
</select>
<br/>
Part 2 of URL:<br/>
<select id="part2">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
</select>
<br/>
<button onClick="generateURL();">Generate URL</button>
<br/>
URL Generated:
<span id="result"></span>
In place of the second dropdown select you can use anything from date to input box.
UPDATE
A date example:
function generateURLX()
{
var partx = document.getElementById('partx').value;
var party = document.getElementById('party').value;
var urlx = "https://example.com/"+partx+"/"+party+".html";
document.getElementById('resultx').innerHTML = urlx;
}
Part 1 of URL:<br/>
<select id="partx">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
</select>
<br/>
Part 2 of URL:<br/>
<input type="date" id="party" placeholde="Date">
<br/>
<button onClick="generateURLX();">Generate URL</button>
<br/>
URL Generated:
<span id="resultx"></span>

Related

Spring, Thyme: redirecting to <select> value

I have a small problem, I just need to redirect to the value of select like:
/results/name/Heli Redel
How would I go about doing that?
<div class="search">
<select th:required="true">
<option th:value="'Eha Raudreha'" th:text="'Eha Raudreha'"></option>
<option th:value="'Heli Redel'" th:text="'Heli Redel'"></option>
<option th:value="'Mait Kuusevaik'" th:text="'Mait Kuusevaik'"></option>
</select>
<a class="buttons" th:href="#{|/results/name/NAME_HERE|}"><button class="deleteBtn">Otsi</button></a><br />
</div>
Add a jQuery function, that when the button is clicked, it redirects to a another url.
jQuery(document).ready(function() {
$('.deleteBtn').on('click', function() {
var selectValue = $('.search').find('select').val()
window.location.replace("/result/name/" + selectValue);
})
})
I would add some ids to your elements to make it easier to fetch data.

Server (jsp) cannot send the data to MYSQL db due to inrecognizable internal errors aren't showing up in stackTrace

I have created a web project that need to register user data in database table that can be used further.
registration.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Registration</title>
<link rel="stylesheet" href="style.css">
<script language="JavaScript" type="text/javascript">
function validateform(theForm) {
var First_Name = theForm.First_Name;
var Last_Name = theForm.Last_Name;
var UserName = theForm.UserName;
var phoneNo = theForm.phoneNo;
var password = theForm.password;
var reenter_password = theForm.reenter_password;
var email = theForm.email;
var gender = theForm.gender;
var title = theForm.title;
var day = theForm.dobday;
var month = theForm.dobmonth;
var year = theForm.dobyear;
var atposition = email.value.indexOf("#");
var dotposition = email.value.lastIndexOf(".");
if (First_Name.value == "") {
alert("Name can't be empty");
First_Name.focus();
return false;
}
if (!First_Name.value.match(/^[A-Za-z]+$/)) {
alert("name contains invalid Characters!");
First_Name.focus();
return false;
}
if (Last_Name.value == "") {
alert("Name can't be empty");
Last_Name.focus();
return false;
}
if (!Last_Name.value.match(/^[A-Za-z]+$/)) {
alert("name contains invalid Characters!");
Last_Name.focus();
return false;
}
/* user name validation */
if (UserName.value == "") {
alert("user Name can't be empty");
UserName.focus();
return false;
}
/* email validation */
if (email.value == "") {
alert("please enter your email");
email.focus();
return false;
}
if (atposition < 1 || dotposition < atposition + 2 || dotposition + 2 >= email.value.length) {
alert("Please enter a valid e-mail address \n atpostion:" + atposition + "\n dotposition:" + dotposition);
email.focus();
return false;
}
/* password validation */
if (password.value == "") {
alert("password can't be empty");
password.focus();
return false;
}
if (password.value.length < 6) {
alert("Password must be at least 6 characters long.");
password.focus();
return false;
}
if (reenter_password.value == "") {
alert("please re-enter the password");
reenter_password.focus();
return false;
}
if (!password.value.match(reenter_password.value)) {
alert("password must be same!");
return false;
}
if (!gender[0].checked && !gender[1].checked) {
alert("please select your gender");
gender.focus();
return false;
}
return true;
}
</script>
</head>
<body id="html">
<center>
<br><br>
<section class="registration_form" >
<form name="registration" method="post" action="registerUser.jsp"
onsubmit="return validateform(this);">
<table id="tab1" bgcolor="#ffffff" width="60%" height="320%">
<td width="10%"></td><td><h1>Register here</h1></td>
<tr><td width="20%"></td><td><label for="First Name">First Name</label>
<input type="First Name" id="usernametxtbox" name="First_Name" placeholder="enter First Name" >
</td></tr>
<tr><td width="20%"></td><td><label for="Last Name">Last Name</label>
<input type="Last Name" id="usernametxtbox" name="Last_Name" placeholder="enter Last Name" >
</td>
</tr>
<tr><td width="20%"></td><td>
<label for="User Name">User Name</label>
<input type="User Name" id="txtbox" name="UserName" placeholder="enter User Name" ></td>
</tr>
<tr><td width="20%"></td>
<td><label for="usermail">Email</label>
<input type="email" id="txtbox" name="email" placeholder="yourname#email.com" ></td></tr>
<tr><td width="20%"></td><td>
<label for="password">Password</label>
<input type="password" id="txtbox" name="password" placeholder="password" ></td></tr>
<tr><td width="20%"></td><td><label for="reenter_password">re-enter Password</label>
<input type="password" id="txtbox" name="reenter_password" placeholder="Re-enter password"></td></tr>
<tr><td width="20%"></td><tr height="10%"><td></td></tr>
<tr><td width="20%"></td><td><label>Date of Birth:</label>
<select name=birthday>
<option> - Month - </option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select name="DOBDay">
<option> - Day - </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="DOBYear">
<option> - Year - </option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
<option value="1979">1979</option>
<option value="1978">1978</option>
<option value="1977">1977</option>
<option value="1976">1976</option>
<option value="1975">1975</option>
<option value="1974">1974</option>
<option value="1973">1973</option>
<option value="1972">1972</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
<option value="1968">1968</option>
<option value="1967">1967</option>
<option value="196\6">1966</option>
<option value="1965">1965</option>
<option value="1964">1964</option>
<option value="1963">1963</option>
<option value="1962">1962</option>
<option value="1961">1961</option>
<option value="1960">1960</option>
<option value="1959">1959</option>
<option value="1958">1958</option>
<option value="1957">1957</option>
<option value="1956">1956</option>
<option value="1955">1955</option>
<option value="1954">1954</option>
<option value="1953">1953</option>
<option value="1952">1952</option>
<option value="1951">1951</option>
<option value="1950">1950</option>
<option value="1949">1949</option>
<option value="1948">1948</option>
<option value="1947">1947</option>
</select></td>
</tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td><td><label>Gender:</label><input type="radio" name="gender" value="male" checked > Male
<input type="radio" name="gender" value="female"> Female</td>
</tr>
<tr><td width="20%"></td><td><input type="submit" value="register" id="butn">
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
</table>
</form>
</section>
</center>
</body>
</html>
After submitted goes to registerUser.jsp
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.ResultSet"%>
<%#page import="java.sql.Statement"%>
<%#page import="java.sql.Connection"%>
<%# page import="java.io.*" %>
<html>
<body>
<%
String firstname=request.getParameter("First_Name");
String lastname=request.getParameter("Last_Name");
String username=request.getParameter("UserName");
String email=request.getParameter("email");
String password=request.getParameter("password");
String month=request.getParameter("birthday");
String day=request.getParameter("DOBday");
String year=request.getParameter("DOBYear");
String gender=request.getParameter("gender");
String DOB=day+"-"+month+"-"+year;
String connectionURL = "jdbc:mysql://localhost:3306/demo?useSSL=false";
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "admin", "admin");
statement = connection.createStatement();
String QueryString = "insert into user_details(FIRST_NAME,LAST_NAME,USERNAME,EMAIL,PASSWORD,DOB,GENDER) values( '"+firstname+"',' " +
lastname+"', '"+username+"', '"+email+"', '"+password+"',' "+DOB+","+gender+");";
statement.executeUpdate(QueryString);
%>
User data inserted successfully
<% }
catch (Exception ex) {ex.printStackTrace();}
finally{
statement.close();
connection.close();
}
%>
</body>
Every thing seems to be fine with variable names,checked if incorrect. Included MYSql connector. Server works perfect.
I doubt in unidentified datatype mismatch in db or any other server-page error, as it isn't coming up in stacktrace, it's hard to find the flaw.
Please help out on this !
I have table structure in MYSQL
I think you had made a mistake in the SQL query
String QueryString = "insert into user_details"+
"(FIRST_NAME,LAST_NAME,USERNAME,EMAIL,PASSWORD,DOB,GENDER) values( '"+
firstname+"',' " + lastname+"', '"+
username+"', '"+email+"', '"+password+"','"+DOB+"','"+gender+"');";
if still not work then change this following code
check this code if work this work for me:
Connection connection;
Statement statement;
String driver = "com.mysql.jdbc.Driver";
String dbURL = "jdbc:mysql://localhost:3306/demoDb";
String login = "root";
String password = "";
try {
Class.forName(driver);
connection = DriverManager.getConnection(dbURL, login,password);
statement = connection.createStatement();
} catch (SQLException e) {
connection = null;
System.out.println("drivernot found");
System.out.println("SQLException:" + e);
} catch (Exception e) {
System.out.println("Exception:" + e);
}

How do Parse the select-menu HTML? using Java

How do we parse below HTML , and get KEY - VALUE separately
<select id="secondary_contact_type" style="width: 150px" onblur="validateField_userContactDetails_SecContactType(this,true)" name="userContactDetails.SecContactType">
<option value="-1"> Select </option>
<option value="1">Self</option>
<option value="2">CA</option>
<option value="3">ERI</option>
<option value="4">TRP</option>
<option value="5">Others</option>
</select>
i used the below code,
Elements options=doc.select("[id=secondary_contact_type]");
for(Element data:options){
System.out.println(data.attr("value"));
System.out.println(data.text());
}
But it is giving only KEY , in one String value, like
Select Self CA ERI TRP Others
You should also select option.
Try this.
Document doc = Jsoup.parse(html);
Elements options=doc.select("select[id=secondary_contact_type] > option");
for(Element data:options){
System.out.println(data.attr("value"));
System.out.println(data.ownText());
}

Fetching only selected values from CrossSelect of Jquery

In below screen shot there are 2 boxes where user will select from left box to right which ever items he want. Now which ever the column selected by the user in the right box. I need only those items..
Below is the screen shot and code.In the below screen shot
i need to get only one,two and three values from right box.So, how to get only selected items from the right box?
To Fetch the right box values is this the right menthod in JavaScript Function. Please help
function export()
{
<%if(request.getParameter("myServersID1") != null)
{
String[] ItemNames;
ItemNames = request.getParameterValues("myServersID1");
for(int i = 0; i < ItemNames.length; i++)
{
String selecteditems;
selecteditems= ItemNames[i];
System.out.println("selecteditems" +selecteditems);
}}%>
alert("selectcol");
}
when button is added
<input type="button" value="Export" class="btn-arrow-pri btn-small" style="margin-left: 300px;" onclick="export();"/>
___ ____
four | |
|> |one
five |< |two
| >> |three
|<< |
____
CODE is:
<html>
[...]
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function moveRight(){
$(document).ready(function(){
$("#myserversID option:selected").appendTo("#myserversID1")
});
}
function moveLeft(){
$(document).ready(function(){
$("#myserversID1 option:selected").appendTo("#myserversID")
});
}
</script>
</head>
<body>
<select size="8" id="myserversID" multiple="multiple">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
<option value="five">five</option>
<option value="six">six</option>
</select>
<input type="button" value="Move Right" onclick="moveRight();"/>
<input type="button" value="Move Left" onclick="moveLeft();"/>
<select size="8" id="myserversID1" multiple="multiple">
</select>
</body>
</html>

display selected value of the drop down in jsp page

I want to display selected value. In the text field I can display it within value like below
value ="<%=event_data.getE_venue()%>"
code :
<input type="text" name="where" placeholder="Add a place" size="23" value ="<%=event_data.getE_venue()%>"/>
<select name="category" value ="<%=event_data.getE_target_category()%>" id="single1">
<option>Sports</option>
<option>Corporate</option>
<option>Religious</option>
<option>Music</option>
</select>
but in dropdown box it doesn't work.
please help me. thanks..
Firstly, select doesn't work in that way , you need to put selected attribute in option that matches your input.
for example:
<option selected='selected'>Sports</option>
check this fiddle :
http://jsfiddle.net/ZLTS7/
your code should be something like :
<input type="text" name="where" placeholder="Add a place" size="23" value ="<%=event_data.getE_venue()%>"/>
<select name="category" id="single1">
<option <%= (event_data.getE_target_category().equals("Sports")?"selected='selected'":"") %>>Sports</option>
<option <%= (event_data.getE_target_category().equals("Corporate")?"selected='selected'":"") %>>Corporate</option>
<option <%= (event_data.getE_target_category().equals("Religious")?"selected='selected'","") %>>Religious</option>
<option <%= (event_data.getE_target_category().equals("Music")?"selected='selected'":"") %>>Music</option>
</select>
You need to change the dropdown value through Javascript or jQuery. You can assign desired value to dropdown just like input
var dd = document.getElementById('single1');
var opts = ddl.options.length;
var value = <%=event_data.getE_venue()%>;
for (var i=0; i<opts; i++){
if (dd.options[i].value == value){
dd.options[i].selected = true;
break;
}
}
or if you are using jQuery.
$("#single1").val(value);
See this example:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#slectboxid option').click(function(){
$('#textboxid').val($(this).val());
});
});
</script>
</head>
<body>
<form action="#" method="post">
<select name="select" id="slectboxid">
<option value="test">test</option>
<option value="test2">test2</option>
</select>
<input type="text" name="text" id="textboxid" />
</form>
</body>
</html>

Categories

Resources