Java Spring dropdown populating - java

I am working in spring mvc, I am doing some jsp with showing multiple dropdowns in a single pages....
I seen an example to show drop down from database by using the following example.
<%# page import="java.util.*" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="state" scope="session" class="src.StateDAO"/>
<html>
<head>
<title></title>
</head>
<body>
<form id="test" method="POST" action="">
<input name="state" type="radio" value="Australia" id="state-aus">Australia
<input name="state" type="radio" value="NewZealand" id="state-new">NewZealand
<input name="state" type="radio" value="India" id="state-oth" >India
<Select name="othStates" size="1" id="oth-states">
<c:forEach items="${state.stateList}" var="st">
<option value="1"><c:out value="${st.name}"/></option>
</c:forEach>
</select>
<br>
<input type="Submit" name="cmdSub" value="SUBMIT">
<input type="Reset" name="cmdReset" value="RESET">
</form>
</body>
</html>
Is this right way to do this to get dropdowns in jsp using Spring mvc?

I think that a better option is to use the spring tags for jsp
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
...
<form:select path="country">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${countryList}" />
</form:select>
See full example here: http://www.mkyong.com/spring-mvc/spring-mvc-dropdown-box-example/
Edit:
$('#stateSelect').change(function() {
$.ajax({
type:"GET",
url : "/getCitiesForState",
data : { state: $('#stateSelect').val()},
success : function(data) {
$('#citySelect').empty(); //remove all child nodes
for(var i = 0; i < data.length; i++){
var newOption = $('<option value=data[i].value>data[i].text</option>');
$('#citySelect').append(newOption);
}
},
error: function() {
alert('Error occured');
}
});
});
On the server side you need an endpoint that respondes on the url(/getCitiesForState in the example) and returns a list of objects that have value and text properties.
Edit(add controlelr):
#Controller
public class HelloController{
#RequestMapping("/getCitiesForState")
#ResponseBody
public List<City> printHello(#RequestParam long state) {
List<City> cities = //get from the some repository by state
return cities;
}
}
public class City{
private String value;
private String text;
//getters setters
}

Related

Include the record in same JSP returned from controller. If record is there then show the record as a table or give a message

Include the record in the same JSP returned from the controller. If any record is there then show the record as a table or give a message
I have done it on two different JSP pages. I need to do it on a single page. I need to update the controller as I am only allowed to return the same jsp.mappings
#Controller
public class EmpController {
#Autowired
EmpDao dao;
#PostMapping("/viewemp")
public String viewemp(#RequestParam(value="start") #DateTimeFormat(pattern="yyyy-MM-dd") Date start,
#RequestParam(value="end") #DateTimeFormat(pattern="yyyy-MM-dd") Date end, Model m){
List<Event> list=dao.getEmployees(start,end);
if(list.size()==0) return "index";
m.addAttribute("list",list);
return "viewemp";
}
}
jsp1:
<center>
<h1> Search Event</h1>
<form method="post" action="viewemp">
Start Date: <input type="date" id="start" name="start"/>
End Date: <input type="date" id="end" name="end"/>
<input type="submit" value="find" />
</form></center>
jsp2:
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<h1>Event List</h1>
<table border="2" width="70%" cellpadding="2">
<tr><th>eventName</th><th>eventOrgName</th><th>eventFare</th><th>startDate</th><th>endDate</th></tr>
<c:forEach var="emp" items="${list}">
<tr>
<td>${emp.eventName}</td>
<td>${emp.eventOrgName}</td>
<td>${emp.eventFare}</td>
<td>${emp.startDate}</td>
<td>${emp.endDate}</td>
</tr>
</c:forEach>
</table>
<br/>
I need to add jsp2 inside jsp1 if it has any record. like this image

Thymeleaf: Update Table on Form Submit

I have a view in which I have a Form to create a new Exercise object, and a table to display all exercises. Now I want that the table automatically refreshes with the newly created exercise. Currently it displays the table as empty, until I manually go to localhost:8080/exercise again.
Here's my controller:
#Controller
public class ExerciseController {
#Autowired
private ExerciseService exerciseService;
#Autowired
private ModelMapper modelMapper;
#GetMapping("/exercise")
public String exerciseView(final Model model) {
List<Exercise> exerciseList = exerciseService.getAllExercises();
model.addAttribute("exerciseDTO", new ExerciseDTO());
model.addAttribute("title", "Create an Exercise");
model.addAttribute("exercises", exerciseList);
return "exercise";
}
#PostMapping("/exercise")
public String createExercise(#ModelAttribute final ExerciseDTO exerciseDto) {
final Exercise exercise = this.modelMapper.map(exerciseDto, Exercise.class);
this.exerciseService.createExercise(exercise);
return "exercise";
}
}
And my thymeleaf template:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="template :: head"></head>
<body>
<header th:replace="template :: navbar"></header>
<h1>Form</h1>
<form action="#" th:action="#{/exercise}" th:object="${exerciseDTO}" method="post">
<p>Name: <input type="text" th:field="*{name}" /></p>
<p>Description: <input type="text" th:field="*{description}" /></p>
<p>Exercise type:
<select th:field="*{type}" id="typeSelector">
<option th:each="type : ${T(com.nsterdt.routinierbackend.data.enums.ExerciseType).values()}"
th:value="${type}" th:text="${type.displayName}">
</option>
</select>
</p>
<p id="bpmRow">BPM: <input type="number" th:field="*{bpm}" id="bpmInput" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
<br>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>BPM</th>
</tr>
<tr th:each="exercise : ${exercises}">
<td th:text="${exercise.name}"></td>
<td th:text="${exercise.description}"></td>
<td th:text="${exercise.type}"></td>
<td th:text="${exercise.bpm}"></td>
</tr>
</table>
</body>
</html>
Now I thought the createExercise method returning "exercise" would call the exerciseView method and thus calling exerciseService.getAllExercises(). Is there a way to achieve this functionality? Or is there an even better way, without reloading the whole page?
To serve up data without page refreshes you'd need a client side technology like Angular or React. Or plain old javascript. But you can't serve up new data to a page in spring mvc w/o page refreshes.
You can use AJAX to send requests from a client side to a server side and receive an answer without refreshing the page.
Unfortunately I don't have enough time and I can't complete the code but you can do something like this:
function submitItems() {
var contextPath = $("meta[name='ctx']").attr("content");
var exerciseDto = {};
exerciseDto.name = $("#name").val();
exerciseDto.description = $("#description").val();
exerciseDto.typeSelector = $("#typeSelector).val();
exerciseDto.bpmInput = $("#bpmInput").val();
$.ajax({
dataType : "json",
type : "post",
url : contextPath + "/exercise",
data : JSON.stringify(exerciseDto),
cache : false,
contentType : "application/json",
beforeSend : function(xhr) {
xhr.setRequestHeader(header, token);
},
success : function(data) {
console.log(data);
//HERE YOU NEED ACTION TO UPDATE TABLE.
},
error : function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR.responseText);
console.log('getJSON request failed! ' + textStatus);
}
});
}
and then your view must be like this:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="template :: head"></head>
<body>
<header th:replace="template :: navbar"></header>
<h1>Form</h1>
<form onsubmit="submitItems();return false;">
<p>Name: <input id="name" type="text" /></p>
<p>Description: <input id="description" type="text" /></p>
<p>Exercise type:
<select th:field="*{type}" id="typeSelector">
<option th:each="type : ${T(com.nsterdt.routinierbackend.data.enums.ExerciseType).values()}"
th:value="${type}" th:text="${type.displayName}">
</option>
</select>
</p>
<p id="bpmRow">BPM: <input type="number" id="bpmInput" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
<br>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>BPM</th>
</tr>
<tr th:each="exercise : ${exercises}">
<td th:text="${exercise.name}"></td>
<td th:text="${exercise.description}"></td>
<td th:text="${exercise.type}"></td>
<td th:text="${exercise.bpm}"></td>
</tr>
</table>
</body>
</html>
Bear in mind that you need to create an JS action that will update the table. There are quite a few ways of doing that (you can push new data to the Datatable or add new content using JS functions).
I hope this will help you understand a bit more how the AJAX works.
PS. You will have to update your controller as well to return the results, in your instance will be
#PostMapping("/exercise")
public createExerciseDomainTYPEHERE createExercise(#RequestBody final ExerciseDTO exerciseDto) {
final Exercise exercise = this.modelMapper.map(exerciseDto, Exercise.class);
//this.exerciseService.createExercise(exercise);
//return "exercise";
return this.exerciseService.createExercise(exercise);
}
You will have to change this line
public createExerciseDomainTYPEHERE createExercise(#RequestBody final ExerciseDTO exerciseDto) {
to your createExercise Domain Type.

Problem with sending id from one jsp to another (update link)

I have a problm with update link for updating my transaction. At one jsp I have a list with transactions, and after pressing Update" which is link i wanted to pass transactionId, update it, save and get back to list. I've used c:param hidden and it doesnt work. I have no idea why it doesnt pass id and autofill forms. It seems like values are passed in session when ive clicked testing transaction:
http://localhost:8080/transaction/addTransaction?transactionId=64&userId=1
Here is transaction Controller:
#Controller
#RequestMapping("/transaction")
public class TransactionController {
#Autowired
TransactionService transactionService;
#Autowired
CategoryService categoryService;
#Autowired
UserService userService;
#GetMapping("/addTransaction")
public String transactionsList(Model theModel){
Transaction transaction = new Transaction();
theModel.addAttribute("user",userService.getAllUsers());
theModel.addAttribute("category", categoryService.getAllCategories());
theModel.addAttribute("newTransaction", transaction);
return "addTransaction";
}
#PostMapping("/saveTransaction")
public String saveTransaction(#ModelAttribute("newTransaction") Transaction
newTransaction,
BindingResult theBindingResult,
HttpServletRequest request) throws
ParseException {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String date = request.getParameter("transactionDate");
LocalDate localDate = LocalDate.parse(date, formatter);
Date formatedDate =
Date.from(localDate.atStartOfDay().toInstant(ZoneOffset.ofHours(-3)));
newTransaction.setTransactionDate(formatedDate);
transactionService.saveTransaction(newTransaction);
return "redirect:/user/userPage";
}
#GetMapping("/deleteTransaction")
public String deleteUser(#RequestParam("transactionId") int idFromTransactionToDelete,
#RequestParam("userId") int loggedUserId,
RedirectAttributes redirectAttributes){
transactionService.deleteTransactionById(idFromTransactionToDelete);
User loggedUser = userService.getUserById(loggedUserId);
redirectAttributes.addFlashAttribute("loggedUser", loggedUser);
return "redirect:/user/userPage";
}
#GetMapping("/updateTransaction")
public String updateUser(#RequestParam("transactionId") int
idFromTransactionToUpdate,
#RequestParam("userId") int loggedUserId,
RedirectAttributes redirectAttributes,
Model theModel){
Transaction transactionToUpdate = transactionService.getSingleTransactionById(idFromTransactionToUpdate);
transactionToUpdate.setUser(userService.getUserById(loggedUserId));
theModel.addAttribute("newTransaction",transactionToUpdate);
theModel.addAttribute("category", categoryService.getAllCategories());
User loggedUser = userService.getUserById(loggedUserId);
redirectAttributes.addFlashAttribute("loggedUser", loggedUser);
redirectAttributes.addFlashAttribute("newTransaction",
transactionToUpdate);
return "addTransaction";
}
}
Transactions list page:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<html>
<head>
<title>User page</title>
</head>
<body>
<c:url var="addTransactionLink" value="/transaction/addTransaction"/>
<c:url var="addFixedTransactionLink"
value="/fixedTransaction/addFixedTransaction"/>
<h1>Welcome ${loggedUser.login} !</h1>
<br>
<h2>Here are your latest transactions:</h2>
<br>
<a href=${addTransactionLink}>
<input type="button" value="Add Transaction"/>
</a>
<a href=${addFixedTransactionLink}>
<input type="button" value="Add Fixed Transaction"/>
</a>
<table>
<tr>
<th>Category</th>
<th>Price</th>
<th>Description</th>
<th>Date</th>
</tr>
<c:forEach var="transaction" items="${userTransactions}">
<c:url var="deleteTransactionLink"
value="/transaction/deleteTransaction">
<c:param name="transactionId"
value="${transaction.transactionId}"/>
<c:param name="userId" value="${loggedUser.id}"/>
</c:url>
<c:url var="updateTransactionLink"
value="/transaction/addTransaction">
<c:param name="transactionId"
value="${transaction.transactionId}"/>
<c:param name="userId" value="${loggedUser.id}"/>
</c:url>
<tr>
<td>${transaction.category.categoryName}</td>
<td>${transaction.moneyAmount}</td>
<td>${transaction.description}</td>
<td>${transaction.transactionDate}</td>
<td>Delete </td>
<td>Update </td>
<td>${transaction.transactionId}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
And add transaction formular:
<%# taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Add Transaction Form</title>
<link rel="stylesheet"
href="${pageContext.request.contextPath}/resources/css/style.css"/>
</head>
<body>
<h1>Add transaction to database:</h1>
<form:form action="saveTransaction" modelAttribute="newTransaction"
method="post">
<form:hidden path="transactionId"/>
${newTransaction.toString()}
${newTransaction.transactionId}
<table>
<tr>
<td><label>Choose category:</label></td>
<td><form:select name="category.id" path="category.id">
<c:forEach items="${category}" var="category">
<form:option value="${category.id}">${category.categoryName}
</form:option>
</c:forEach>
</form:select>
</tr>
<tr>
<td><label>Choose user:</label></td>
<td><form:select name="user.id" path="user.id">
<c:forEach items="${user}" var="user">
<form:option value="${user.id}">${user.login}
</form:option>
</c:forEach>
</form:select>
</tr>
<tr>
<td><label>Amount:</label></td>
<td><form:input path="moneyAmount" />
</td>
</tr>
<tr>
<td><label>Transaction date:</label></td>
<td><form:input type = "date" path="transactionDate"/>
</td>
</tr>
<tr>
<td><label>Description:</label></td>
<td><form:input path="description" />
</td>
</tr>
<tr>
<label></label>
<td><input type="submit" value="Submit" class="save"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
Problem seems to be in your code. You have defined in jsp:
<c:url var="updateTransactionLink"
value="/transaction/addTransaction">
while in java code:
#GetMapping("/updateTransaction")
public String updateUser(#RequestParam("transactionId") int
In JSP Change it to :
<c:url var="updateTransactionLink"
value="/transaction/updateTransaction">

Calling method onClick of Jsp form Jsp

I wish to call a method in JSP onClick, the method is on the same JSP inside scriptlet.
How should I archive this?
<%# page import="java.io.*,java.lang.*,java.util.*,java.net.*,java.util.*,java.text.*"%>
<%# page import="javax.activation.*,javax.mail.*,org.apache.commons.*"%>
<%# page import="javax.servlet.http.*,javax.servlet.*"%>
<%!
public String sendMail(String to, String sub, String msg) {
String res = null;
System.out.println("HI");
return res;
}%>
<html>
<head>
<title>Send Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<form>
<label>Email To</label><br />
<input type="text" name="to" /><br />
<label>Subject</label><br />
<input type="text" name="sub" /><br />
<label for="body">Message</label><br />
<input type="text" name="msg" /><br />
<input type="submit" onClick="sendMail( to, sub, msg )"/>
</form>
</body>
</html>
Note
The methods name is "sendMail", It's called on submit button
I want to do the whole code in JSP only.
The onclick event occurs when the user clicks on an element. This attribute has the ability to call JS functions (front end)
In your case, you want to call a JAVA function (server side) so the best way is to move the java code to a servlet and use it.
Anyway if you want to keep the JAVA function in the jsp, you can do this via ajax in this way
<script type="text/javascript">
$(document).ready(function() {
$('#sendMailBtn').click(function (){
$.ajax({
type: "post",
url: "/path",
data: "email=" + $('#email').val() + "&subject="+$('#subject').val() + "&msg=" + $('#msg').val(),
success: function(msg){
//
}
});
});
});
</script>
AJAX is a developer's dream, because you can
Update a web page without reloading the page
Request data from a server - after the page has loaded
Receive data from a server - after the page has loaded
Send data to a server - in the background
Check the full code here
<%# page import="java.io.*,java.lang.*,java.util.*,java.net.*,java.util.*,java.text.*"%>
<%# page import="javax.activation.*,javax.mail.*,org.apache.commons.*"%>
<%# page import="javax.servlet.http.*,javax.servlet.*"%>
<%!
public String sendMail(String to, String sub, String msg) {
String res = null;
System.out.println("HI");
return res;
}
%>
<html>
<head>
<title>Send Email using JSP</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<form>
<label>Email To</label><br />
<input id="email" type="text" name="to" /><br />
<label>Subject</label><br />
<input id="subject" type="text" name="sub" /><br />
<label for="body">Message</label><br />
<input id="msg" type="text" name="msg" /><br />
<input id="sendMailBtn" type="submit" />
</form>
</body>
<script type="text/javascript">
$(document).ready(function() {
$('#sendMailBtn').click(function (){
$.ajax({
type: "post",
url: "/path",
data: "email=" + $('#email').val() + "&subject="+$('#subject').val() + "&msg=" + $('#msg').val(),
success: function(msg){
//
}
});
});
});
</script>
</html>
For more information check
AJAX Introduction: http://www.w3schools.com/xml/ajax_intro.asp
onclick Event: http://www.w3schools.com/tags/ev_onclick.asp
JSP- Executes on Server.
JavaScript - executes in browser.
No you cannot call that JSP magically from JS. However you can send an Ajax request or post the form to jsp. BTW, I strongly suggest you to move the java code to a servlet and use it.
This is what I ended up doing
<%# page import= "java.io.*,java.lang.*,java.util.*,java.net.*,java.util.*,java.text.*"%>
<%# page import="javax.activation.*,javax.mail.*,org.apache.commons.*"%>
<%# page import="javax.servlet.http.*,javax.servlet.*"%>
<%!
public String sendMail(String to, String sub, String msg) {
String res = null;
System.out.println("HI");
return res;
}
%>
<%
String a = request.getParameter("to");
if(a != null){
sendMail(request.getParameter("to"),request.getParameter("sub"),request.getParameter("msg"));
}
%>
<html>
<head>
<title>Send Email using JSP</title>
</head>
<body><center>
<form action="#" method="post">
<label>Email To</label><br />
<input type="text" name="to" /><br /> <br />
<label>Subject</label><br />
<input type="text" name="sub" /><br /> <br />
<label for="body">Message</label><br />
<input type="text" name="msg" /><br /> <br />
<input type="submit"/>
</form>
</center></body>
</html>
The action="#" reloads the page, and there is a if condition which calls the required method if the parameter is not blank( Please keep in mind that by default on first call the parameter will be null ).

form submission through jquery

here i am trying to submit a form request through jquery but i don't know why i am unable to do so it just executes the code and nothing is visible on my console.when i try to submit form normally through form action it is successful.any help is thank full.
Jquery & jsp form:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.util.List,beans.Country,mainclasses.CountryListing" errorPage=""%>
<!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>Post a property</title>
<jsp:useBean id="CNY" class="beans.Country" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript" src="js/combochange.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#contact_details_submit").click(function() {
var companyname = $("#company_name").val();
var officeaddress = $("#office_address").val();
var countryname = $("#country_name option:selected").val();
var statename = $("#state_name option:selected").val();
var cityname = $("#city_name option:selected").val();
var mobile_num = $("#mobilenum").val();
alert(companyname+" : "+officeaddress);
$.get("conatctDetailsInsert.jsp",
{
company_name : companyname,
office_address:officeaddress,
country_name:countryname,
state_name:statename,
city_name:cityname,
mobilenum:mobile_num} ,function(data){
alert(data);
});//end get
});
});
</script>
</head>
<body>
<form action="">
<table cellpadding="0" cellspacing="0" border="1" width="500">
<tbody id="contact_details">
<tr>
<td>i am a
<select>
<option>Agent/broker</option>
<option>Builder/Pvt.Ltd company</option>
</select>
</td>
</tr>
<tr>
<td><h3>Contact Details</h3></td></tr>
<tr>
<td>
Company Name:<input type="text" value="" id="company_name" name="company_name"/>
</td></tr>
<tr>
<td>Office Address:<input type="text" value="" id="office_address" name="office_address"/><br>
Country:
<select id="country_name" name="country_name">
<option>-Select-</option>
<%
mainclasses.CountryListing CNY_CL = new mainclasses.CountryListing();
List<Country> CNY_List=CNY_CL.getCountry();
for(int i=0; i < CNY_List.size(); i++ ){
CNY=(beans.Country)CNY_List.get(i);
%>
<option value="<%=CNY.getIdCountry() %>"><%=CNY.getCountryName() %></option>
<%} %>
</select><br>
State:<select id="state_name" name="state_name"><option></option></select><br>
City:<select id="city_name" name="city_name"><option></option></select><br>
</td>
</tr>
<tr>
<td>
Contact Number:
<input type="tel" id="mobilenum" value="" name="mobilenum"/>
</td>
</tr>
<tr>
<td>
<input type="submit" id="contact_details_submit" name="contact_details_submit"/>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
Request handling jsp:
<%#page import="beans.ConatctDetailsService"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%boolean x=false;
String company_name =request.getParameter("company_name");
String office_address =request.getParameter("office_address");
String country_name =request.getParameter("country_name");
String state_name =request.getParameter("state_name");
String city_name =request.getParameter("city_name");
String mobilenum =request.getParameter("mobilenum");
beans.ConatctDetailsService CTD = new beans.ConatctDetailsService();
CTD.setCompanyName(company_name);
CTD.setCompanyName(office_address);
CTD.setIdCountry(country_name);
CTD.setIdState(state_name);
CTD.setCity(city_name);
CTD.setMobNum(mobilenum);
x=CTD.insert();
System.out.println(x);
CTD.geterror();
if(x){
out.println("done");
}
else{
out.println("no");
}
%>
Try adding an id to the form tag e.g.
<form id='myForm'>
And change to a on form submit.
$(document).ready(function(){
$("#myForm").on('submit', function(e) {
e.preventDefault();
try putting AJAX request to check what's happening:
$.ajax({
type:'GET',
url: 'conatctDetailsInsert.jsp',
data: {company_name : companyname,
office_address:officeaddress,
country_name:countryname,
state_name:statename,
city_name:cityname,
mobilenum:mobile_num},
success: function(data) {
console.log(data);
}, error: function(jqXHR, textStatus, errorThrown) {
console.log(err);
}
});
I would do it this way, much cleaner.
$("form").submit(function(e) {
var formData = $(this).serialize();
$.get("conatctDetailsInsert.jsp",formData, function(data){
alert(data);
});//end get
return false;
});

Categories

Resources