我有一个用于添加、删除、更新和搜索功能的 servlet。我在 jquery 上传递参数。根据这个参数,我将逻辑放在我的 servlet 中。
问题是所有其他功能都在工作。但是当我在动作和 servlet 条件中输入相同的名称时,我得到 url not found 错误。如果我使用不同的名称,则不会出现未找到错误。我在下面添加了我的 jquery 和 servlet 代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
$(document).ready(function() {
$("#doctorId").keypress(function (e)
{
//if the letter is not digit then display error and don't type anything
if( e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57))
{
//display error message
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});
$('.btn-submit').click(function(e) {
required = ["doctorId","speciality","employeeId","experience","qualification"];
// Declare the function variables:
// Parent form, form URL, email regex and the error HTML
var $formId = $(this).parents('form');
var formAction = $formId.attr('action');
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var nicReg=/^[0-9]{9}[vVxX]$/;
var $error = $('<span class="error"></span>');
// Prepare the form for validation - remove previous errors
$('li',$formId).removeClass('error');
$('span.error').remove();
// Validate all inputs with the class "required"
$('.required',$formId).each(function(){
for (i=0;i<required.length;i++) {
var inputVal = $('#'+required[i]).val();
var $parentTag = $('#'+required[i]).parent();
if(inputVal == ''){
$parentTag.addClass('error').append($error.clone().text('Required Field'));
}
}
// Run the email validation using the regex for those input items also having class "email"
if($(this).hasClass('nic') == true){
var inputVal2=$('#nic').val();
var $parentTag2=$('#nic').parent();
if(!nicReg.test(inputVal2)){
$parentTag2.addClass('error').append($error.clone().text('Enter valid NIC'));
}
}
// Check passwords match for inputs with class "password"
if($(this).hasClass('password') == true){
var password1 = $('#password-1').val();
var password2 = $('#password-2').val();
if(password1 != password2){
$parentTag.addClass('error').append($error.clone().text('Passwords must match'));
}
}
});
// All validation complete - Check if any errors exist
// If has errors
if ($('span.error').length > 0) {
$('span.error').each(function(){
// Set the distance for the error animation
var distance = 5;
// Get the error dimensions
var width = $(this).outerWidth();
// Calculate starting position
var start = width + distance;
// Set the initial CSS
$(this).show().css({
display: 'block',
opacity: 0,
right: -start+'px'
})
// Animate the error message
.animate({
right: -width+'px',
opacity: 1
}, 'slow');
});
} else {
this.form.action = "DoctorServlet?method=add";
$formId.submit();
}
// Prevent form submission
e.preventDefault();
});
// Fade out error message when input field gains focus
$('.required').focus(function(){
var $parent = $(this).parent();
$parent.removeClass('error');
$('span.error',$parent).fadeOut();
});
$('.btn-delete').click(function(e){
// Declare the function variables:
// Parent form, form URL, email regex and the error HTML
var $formId = $(this).parents('form');
var formAction = $formId.attr('action');
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var $error = $('<span class="error"></span>');
// Prepare the form for validation - remove previous errors
$('li',$formId).removeClass('error');
$('span.error').remove();
// Validate all inputs with the class "required"
$('.required',$formId).each(function(){
var inputVal = $("#doctorId").val();
var $parentTag = $("#doctorId").parent();
if(inputVal == ''){
$parentTag.addClass('error').append($error.clone().text('Required Field'));
}
// Run the email validation using the regex for those input items also having class "email"
// Check passwords match for inputs with class "password"
});
// All validation complete - Check if any errors exist
// If has errors
if ($('span.error').length > 0) {
$('span.error').each(function(){
// Set the distance for the error animation
var distance = 5;
// Get the error dimensions
var width = $(this).outerWidth();
// Calculate starting position
var start = width + distance;
// Set the initial CSS
$(this).show().css({
display: 'block',
opacity: 0,
right: -start+'px'
})
// Animate the error message
.animate({
right: -width+'px',
opacity: 1
}, 'slow');
});
} else {
this.form.action = "DoctorServlet?method=delete";
$formId.submit();
}
// Prevent form submission
e.preventDefault();
});
// Fade out error message when input field gains focus
$('.required').focus(function(){
var $parent = $(this).parent();
$parent.removeClass('error');
$('span.error',$parent).fadeOut();
});
$('.btn-update').click(function(e){
required = ["doctorId","experience","qualification"];
// Declare the function variables:
// Parent form, form URL, email regex and the error HTML
var $formId = $(this).parents('form');
var formAction = $formId.attr('action');
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var $error = $('<span class="error"></span>');
// Prepare the form for validation - remove previous errors
$('li',$formId).removeClass('error');
$('span.error').remove();
// Validate all inputs with the class "required"
$('.required',$formId).each(function(){
for (i=0;i<required.length;i++){
var inputVal = $('#'+required[i]).val();
var $parentTag = $('#'+required[i]).parent();
if(inputVal == ''){
$parentTag.addClass('error').append($error.clone().text('Required Field'));
}
}
// Run the email validation using the regex for those input items also having class "email"
// Check passwords match for inputs with class "password"
});
// All validation complete - Check if any errors exist
// If has errors
if ($('span.error').length > 0) {
$('span.error').each(function(){
// Set the distance for the error animation
var distance = 5;
// Get the error dimensions
var width = $(this).outerWidth();
// Calculate starting position
var start = width + distance;
// Set the initial CSS
$(this).show().css({
display: 'block',
opacity: 0,
right: -start+'px'
})
// Animate the error message
.animate({
right: -width+'px',
opacity: 1
}, 'slow');
});
} else {
this.form.action = "DoctorServlet?method=update";
$formId.submit();
}
// Prevent form submission
e.preventDefault();
});
// Fade out error message when input field gains focus
$('.required').focus(function(){
var $parent = $(this).parent();
$parent.removeClass('error');
$('span.error',$parent).fadeOut();
});
$('.btn-search').click(function(e){
// Declare the function variables:
// Parent form, form URL, email regex and the error HTML
var $formId = $(this).parents('form');
var formAction = $formId.attr('action');
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var $error = $('<span class="error"></span>');
// Prepare the form for validation - remove previous errors
$('li',$formId).removeClass('error');
$('span.error').remove();
// Validate all inputs with the class "required"
$('.required',$formId).each(function(){
var inputVal = $("#doctorId").val();
var $parentTag = $("#doctorId").parent();
if(inputVal == ''){
$parentTag.addClass('error').append($error.clone().text('Required Field'));
}
// Run the email validation using the regex for those input items also having class "email"
// Check passwords match for inputs with class "password"
});
// All validation complete - Check if any errors exist
// If has errors
if ($('span.error').length > 0) {
$('span.error').each(function(){
// Set the distance for the error animation
var distance = 5;
// Get the error dimensions
var width = $(this).outerWidth();
// Calculate starting position
var start = width + distance;
// Set the initial CSS
$(this).show().css({
display: 'block',
opacity: 0,
right: -start+'px'
})
// Animate the error message
.animate({
right: -width+'px',
opacity: 1
}, 'slow');
});
} else {
this.form.action = "DoctorServlet?method=search";
$formId.submit();
}
// Prevent form submission
e.preventDefault();
});
// Fade out error message when input field gains focus
$('.required').focus(function(){
var $parent = $(this).parent();
$parent.removeClass('error');
$('span.error',$parent).fadeOut();
});
});
public class DoctorServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sDocId = request.getParameter("doctorId");
Integer dId = Integer.parseInt(sDocId);
String speciality = request.getParameter("speciality");
String experience = request.getParameter("experience");
String qualification = request.getParameter("qualification");
String sempId = request.getParameter("employeeId");
String action = request.getParameter("method");
Doctors d = new Doctors();
if (action.equalsIgnoreCase("add")) {
Integer empId = Integer.parseInt(sempId);
d.setDocId(dId);
d.setEmpId(empId);
d.setExp(experience);
d.setSumOfQn(qualification);
d.setSpeciality(speciality);
try {
boolean result = new Doctors().insertDoctor(d);
if (result == true) {
request.setAttribute("add", "success");
}
request.getRequestDispatcher("doctor_result.jsp").forward(request, response);
// response.sendRedirect("doctor_result.jsp");
} catch (SQLException ex) {
Logger.getLogger(DoctorServlet.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (action.equalsIgnoreCase("delete")) {
d.setDocId(dId);
try {
boolean result = d.removeDoctor(dId);
if (result == true) {
request.setAttribute("delete", "success");
}
request.getRequestDispatcher("doctor_result.jsp").forward(request, response);
response.sendRedirect("doctor_result.jsp");
} catch (SQLException ex) {
Logger.getLogger(DoctorServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if(action.equalsIgnoreCase("update")){
try {
boolean result=d.updateDoctor(dId, experience, qualification);
if(result==true){
request.setAttribute("update", "success");
}
request.getRequestDispatcher("doctor_result.jsp").forward(request, response);
} catch (SQLException ex) {
Logger.getLogger(DoctorServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if(action.equalsIgnoreCase("search")){
try {
ResultSet rs=d.searchDoctor(dId);
while(rs.next()){
int docId=rs.getInt(1);
request.setAttribute("docId", docId);
String exp=rs.getString(3);
request.setAttribute("exp",exp);
String spec=rs.getString(2);
request.setAttribute("speciality", spec);
String qlf=rs.getString(5);
request.setAttribute("qlf", qlf);
}
request.getRequestDispatcher("doctor_result").forward(request, response);
} catch (SQLException ex) {
Logger.getLogger(DoctorServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}