I'm having a problem that when my JSP loads, it returns a blank page. The page is supposed to call the Controller with the parameter "btnListar" and call de DAO to list entries from the database. I debugged the code (I'm using NetBeans to build this app), and the values are being passed correctly, the variables are filled with entries from the DB that the DAO gets, but the page the browser is supposed to show just doesn't appear, it's blank.
My Controller:
public class ExperienciaControle extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String action = request.getParameter("action");
switch (action) {
case "btnCadastrar": {
Experiencia exp = new Experiencia();
exp.setEmpresa(request.getParameter("txtEmpresa"));
exp.setCargo(request.getParameter("txtCargo"));
exp.setData(request.getParameter("txtData"));
exp.setAtribuicoes(request.getParameter("txtAtrib"));
ExperienciaDAO dao = new ExperienciaDAO();
dao.insertExp(exp);
break;
}
case "btnAtualizar": {
Experiencia exp = new Experiencia();
exp.setEmpresa(request.getParameter("txtEmpresa"));
exp.setCargo(request.getParameter("txtCargo"));
exp.setData(request.getParameter("txtData"));
exp.setAtribuicoes(request.getParameter("txtAtrib"));
ExperienciaDAO dao = new ExperienciaDAO();
dao.updateExp(exp);
break;
}
case "btnRemover": {
Experiencia exp = new Experiencia();
exp.setId(Integer.parseInt(request.getParameter("chckId")));
ExperienciaDAO dao = new ExperienciaDAO();
dao.deleteExp(exp);
break;
}
case "btnListar": {
ExperienciaDAO dao = new ExperienciaDAO();
ArrayList<Experiencia> expArray = dao.selectExp();
request.setAttribute("expArray", expArray);
break;
}
default:
break;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
#Override
public String getServletInfo() {
return "Short description";
}
}
My JSP:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# page import="java.util.*" %>
<%# page import="Modelo.*" %>
<!DOCTYPE html>
<html>
<head>
<script src="../js/dropdown.js"></script>
<script src="../js/jquery-1.12.3.min.js"></script>
<script src="../js/lista.js"></script>
<link type="text/css" rel="stylesheet" href="../css/bootstrap.css" />
<link type="text/css" rel="stylesheet" href="../css/admin.css" />
<link type="text/css" rel="stylesheet" href="../css/dropdown.css" />
<link type="text/css" rel="stylesheet" href="../css/body.css" />
<link type="text/css" rel="stylesheet" href="../css/main.css" />
<link type="text/css" rel="stylesheet" href="../css/fonts.css" />
<title>Currículo | Administração</title>
</head>
<body>
<div class="body">
<div>
<nav id="primary_nav_wrap">
<ul>
<li class="current-menu-item">Home</li>
<li><a>Experiência</a>
<ul>
<li>Adicionar</li>
<li>Listar</li>
</ul>
</li>
<li><a>Educação</a>
<ul>
<li>Adicionar</li>
<li>Atualizar</li>
<li>Remover</li>
</ul>
</li>
<li><a>Habilidade</a>
<ul>
<li>Adicionar</li>
<li>Atualizar</li>
<li>Remover</li>
</ul>
</li>
</ul>
</nav>
</div>
<div id="divAddExp" style="display:none" class="cadastros">
<form name="frmExp" action="../ExperienciaControle" method="POST">
<h1 style="font-family: Roboto">Cadastro de Experiência</h1><br />
<label for="lblEmpresa">Empresa </label><input type="text" name="txtEmpresa" class="form-control" /><br />
<label for="lblCargo">Cargo </label><input type="text" name="txtCargo" class="form-control" /><br />
<label for="lblData">Data </label><input type="text" name="txtData" class="form-control" /><br />
<label for="lblAtrib">Atribuições </label><textarea rows="4" cols="50" name="txtAtrib" class="form-control"></textarea><br />
<input class="btn" type="submit" name="btnCadastrar" value="Cadastrar" />
</form>
</div>
<div id="divUpdExp" class="cadastros">
<form name="frmExp" action="/ExperienciaControle" method="GET">
<h1 style="font-family: Roboto">Listagem de Experiência</h1><br />
<table>
<thead>
<tr>
<th> </th>
<th>ID</th>
<th>Empresa</th>
<th>Cargo</th>
</tr>
</thead>
<%
request.getRequestDispatcher("/ExperienciaControle?action=btnListar").forward(request, response);
ArrayList<Experiencia> expArray = (ArrayList<Experiencia>) request.getAttribute("expArray");
for (Experiencia exp : expArray) {
%>
<tr>
<td><input type="checkbox" name="chckID" value="<%= exp.getId()%>" /></td>
<td><%= exp.getId()%></td>
<td><%= exp.getEmpresa()%></td>
<td><%= exp.getCargo()%></td>
</tr>
<%
}
%>
</table>
<span><input class="btn" type="submit" name="btnListar" value="Listar" /></span>
<span><input class="btn" type="submit" name="btnAtualizar" value="Atualizar" /></span>
<span><input class="btn" type="submit" name="btnRemover" value="Remover" /></span>
</form>
</div>
</div>
</body>
</html>
If I remove the Java code from the JSP and turn it into a pure HTML page, it works perfectly, the page comes up without any problems. Tomcat (and Glassfish) log shows no error messages, which makes things even harder. :(
What am I doing wrong here? Why am I getting a blank page?
COMPLETE CODE: http://gitlab.creationkit.com.br/thales/Curriculum
Related
This question already has an answer here:
How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
(1 answer)
Closed 2 years ago.
I'm having a problem with my code. I'm making a dealership website. I'm getting a null value for my attribute. My program runs through a foreach loop of my ArrayList on my JSP, I also have buttons on each car to purchase them. When the user chooses purchase on that item its suppose to fetch the car that they chose and forward it to a purchase page. But for some odd reason when I get to the purchase page the car value is null. Any help will be appreciated.
Servlet:
package com.servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.dealership.Car;
import com.dealership.Inventory;
/**
* Servlet implementation class PurchaseServlet
*/
#WebServlet("/PurchaseServlet")
public class PurchaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public PurchaseServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession(true);
Car purchaseCar = (Car)session.getAttribute("purchaseCar");
Inventory inventory = new Inventory();
if(purchaseCar == null) {
purchaseCar = new Car();
}
if (request.getParameter("purchase") != null) {
inventory.test(purchaseCar);
}
session.setAttribute("purchaseCar", purchaseCar);
RequestDispatcher rs = request.getRequestDispatcher("purchase.jsp");
rs.forward(request, response);
}
}
JSP:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:include page="/HomepageServlet" />
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
crossorigin="anonymous">
<!-- Local CSS -->
<link rel="stylesheet" href="styles.css" />
<!-- Link to font -->
<link rel="preconnect" href="https://fonts.gstatic.com">
<link
href="https://fonts.googleapis.com/css2?family=Cinzel:wght#600&display=swap"
rel="stylesheet">
<title> Tropical Auto</title>
</head>
<body>
<div>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Nick's Tropical Autocenter</a> <a
class="navbar-brand" href="#"> <img src="mango.svg" width="30"
height="30" alt="" loading="lazy">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarNav" aria-controls="navbarNav"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active"><a class="nav-link"
href="index.jsp"> INVENTORY <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item"><a class="nav-link" href="usedcars.jsp">USED
INVENTORY</a></li>
<li class="nav-item"><a class="nav-link" href="specials.jsp">SPECIALS</a>
</li>
<li class="nav-item"><a class="nav-link" href="#">ABOUT US</a>
</li>
</ul>
<form class="d-flex" action="SearchCarsServlet" method="post">
<input class="form-control me-2" type="search" name="searchInput"
placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</nav>
</div>
<div class=album py-5bg-light">
<div class="container">
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
<c:forEach var="car" items="${inventory.carList}">
<c:if
test="${car.used == false && car.residence < 120 && car.purchased == false}">
<div class="col">
<div class="card" style="width: 18rem;">
<img src=${car.image } class="card-img-top" alt="...">
<div class="card-body">
<p><span class="mandatory">${car.make}${car.model}</span></p>
<p class="card-text">${car.description}
</div>
<div class="d-flex justify-content-center align-items-center">
<form action="PurchaseServlet" method="POST">
<c:set var="purchaseCar" value="${car}" scope="application" />
<button type="submit" name="purchase" class="btn btn-success">Purchase</button>
</form>
<div class="dropdown">
<a class="btn btn-secondary dropdown-toggle" href="#"
role="button" id="dropdownMenuLink" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false"> Details </a>
<div class="dropdown-menu details-button"
aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" href="#">Make: ${car.make}</a> <a
class="dropdown-item" href="#">Model: ${car.model}</a> <a
class="dropdown-item" href="#">Year: ${car.year}</a> <a
class="dropdown-item" href="#">Mileage: ${car.mileage}</a> <a
class="dropdown-item" href="#">Price: $${car.price}</a>
</div>
</div>
</div>
</div>
</div>
</c:if>
</c:forEach>
</div>
</div>
</div>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
crossorigin="anonymous"></script>
</body>
</html>
It's a lot of code to comprehend but I would say the problem is here:
<foreach>
<form>
<c:set var="purchaseCar" value="${car}" scope="application" />
<submit...
</form>
</foreach>
Session scope will be enough here. But the real problem is this is executed for all records and each is overriding the previous one. So the purchaseCar variable is modified on each iteration.
Usually in a form you want to have <input> tags that sends the needed information with the request to the servlet. Usually that would be the car's ID :)
I have trouble with CSS in JSP pages.
CSS files are not loaded in any page.
I have this JSP page which is a registration form for a student.
I have a page works correctly but that page has no information from my database.
The rest of pages include some lines and some data from the database.
I really don't know where the problem is so I need your help :(
Student form :
<%#page import="java.util.List"%>
<%#page import="tables.Optionn"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="${pageContext.request.contextPath}">
<link href="Style/bootstrap.css" rel="stylesheet" type="text/css">
<link href="Style/font-awesome.css" rel="stylesheet" />
<link href="Style/style.css" rel="stylesheet">
<link href="Style/style-responsive.css" rel="stylesheet">
<% List<Optionn> option = (List<Optionn>) request.getAttribute("option_list");%>
<title>Etudiant</title>
</head>
<body>
<div class="container" id="registration_form">
<form method="post" class="form-login" action="//localhost:8086/GestionPfe/reg_etudiant">
<h2 class="form-login-heading" >ajouter un Etudiant!</h2>
<div class="login-wrap">
Option: <br> <select name="opt" class="form-control">
<%for (Optionn tempop : option) {%>
<option value=<%=tempop.getIdOption()%> > <%=tempop.getNomOption()%></option>
<%}%>
</select><br>
Nom: <br> <input name="nom_etudiant" type="text" class="form-control">
<br>
Prenomt: <br> <input name="prenom_etudiant" type="text" class="form-control">
<br>
Niveau: <br> <input name="niveau_etudiant" type="text" class="form-control">
<br>
Date De Naissance: <br><input name="date_naissance_etudiant" type="date" class="form-control">
<br>
Numero D'inscription: <br> <input name="num_inscription_etudiant" type="text" class="form-control">
<br>
Adresse: <br> <input name="adresse_etudiant" type="text" class="form-control">
<br>
Email: <br><input name="email_etudiant" type="email" class="form-control">
<br>
Mot De Pass: <br><input name="mot_de_pass_etudiant" type="password" class="form-control">
<br>
Telephone: <br><input name="telephone_etudiant" type="text" class="form-control">
<br>
<button type="submit" class="btn btn-theme btn-block"><i class="fa fa-lock"></i> SIGN IN</button>
</div>
</form>
</div>
</body>
</html>
Student Servlet :
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
String op= request.getParameter("opt");
String nom_etudiant= request.getParameter("nom_etudiant");
String prenom_etudiant= request.getParameter("prenom_etudiant");
String niveau_etudiant= request.getParameter("niveau_etudiant");
String num_inscription_etudiant= request.getParameter("num_inscription_etudiant");
String date_naissance_etudiant= request.getParameter("date_naissance_etudiant");
String adresse_etudiant = request.getParameter("adresse_etudiant");
String email_etudiant = request.getParameter("email_etudiant");
String mot_de_pass_etudiant = request.getParameter("mot_de_pass_etudiant");
String telephone_etudiant = request.getParameter("telephone_etudiant");
try {
int idop=Integer.parseInt(op);
int tel=Integer.parseInt(telephone_etudiant);
SimpleDateFormat simpledate=new SimpleDateFormat("yyy-MM-dd");
Date birthdayDate = simpledate.parse(date_naissance_etudiant);
Optionn opt = new Optionn();
opt = (Optionn) session.get(Optionn.class,idop);
Etudiant etudiant=new Etudiant(opt,nom_etudiant,prenom_etudiant,niveau_etudiant, mot_de_pass_etudiant,num_inscription_etudiant,birthdayDate,adresse_etudiant,email_etudiant,tel);
session.save(etudiant);
session.getTransaction().commit();
session.close();
response.sendRedirect("Jsp/home.jsp");
} catch (ParseException ex) {
Logger.getLogger(reg_agent.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Add ".." to all your path,
<link href="../Style/style.css" rel="stylesheet">
Try this :
try {
int idop=Integer.parseInt(op);
int tel=Integer.parseInt(telephone_etudiant);
SimpleDateFormat simpledate=new SimpleDateFormat("yyy-MM-dd");
Date birthdayDate = simpledate.parse(date_naissance_etudiant);
Optionn opt = new Optionn();
opt = (Optionn) session.get(Optionn.class,idop);
Etudiant etudiant=new Etudiant(opt,nom_etudiant,prenom_etudiant,niveau_etudiant, mot_de_pass_etudiant,num_inscription_etudiant,birthdayDate,adresse_etudiant,email_etudiant,tel);
session.save(etudiant);
session.getTransaction().commit();
session.close();
RequestDispatcher dispatcher = request.getRequestDispatcher("Jsp/home.jsp");
dispatcher.forward(request, response);
} catch (ParseException ex) {
Logger.getLogger(reg_agent.class.getName()).log(Level.SEVERE, null, ex);
}
Below is my code where i would be using to pass data to another domain. I am having this problem where with request.getParameter("accType"); the value could not be retrieved. However , other value is working fine, the names are correct and form has a post method on it, can anybody help me with this? Thanks in advance.
<%--
Document : viewEmployee
Created on : Jan 23, 2018, 12:06:44 AM
Author : AaronLee
--%>
<%#page import="java.sql.ResultSet"%>
<%# page import = "da.employeeDA" %>
<jsp:useBean id="employeeDA" class="da.employeeDA" scope="application" ></jsp:useBean>
<jsp:setProperty name="employeeDA" property="*" />
<%--import domain page--%>
<%# page import = "domain.employee" %>
<jsp:useBean id="employee" class="domain.employee" scope="application" ></jsp:useBean>
<jsp:setProperty name="employee" property="*" />
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%!
ResultSet rs = null;
%>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<%--import css--%>
<link href="font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
<link href="css/website.css" rel="stylesheet" type="text/css"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%--Bootstrap CSS--%>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="HandheldFriendly" content="true">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
<title>Account Details</title>
</head>
<body>
<div class="wrapper">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="homepage.jsp">Document</a>
</div>
<ul class="nav navbar-nav">
<li>+ Document</li>
<li class="active">+ Claim</li>
<li>Search Document</li>
<li>Search Claim</li>
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Account<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>Log Out</li>
</ul>
</li>
</ul>
</div>
</nav>
<div class="content">
<%
//login session
String username = null;
if (session.getAttribute("username") == null) {
response.sendRedirect("Login.jsp");
username = null;
return;
} else {
username = session.getAttribute("username").toString();
}
%>
<% try {
rs = employeeDA.searchEmployee(username);
} catch (Exception ex) {
ex.getMessage();
}
if (rs.next()) {
%>
<form method="POST" action="viewEmployee.jsp" >
Account Type :<br/>
<input type="text" value="<%=rs.getString("ACCTYPE")%>" name="accType" disabled /><br/><br/>
Employee ID :<br/>
<input type="text" value="<%=rs.getString("EMPLOYEEID")%>" name="employeeID" disabled/><br/><br/>
Name : <br/>
<input type="text" value="<%=rs.getString("EMPLOYEENAME")%>" name="employeeName"/><br/><br/>
Contact Number :<br/>
<input type="text" value="<%=rs.getString("EMPLOYEECONTACTNO")%>" name="employeeContactNo"/><br/><br/>
<div class="panel-group" id="panelGrp">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse1">Password</a>
</h4>
</div>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">Current Password :<br/>
<input type="text" class="form-control"/><br/>
New Password : <br/>
<input type="password" class="form-control" name="password"/><br/>
Confirm Password : <br/>
<input type="password" class="form-control" /><br/>
<input type="submit" value="Change Password" name="selection" class="btn btn-primary btn-lg btn-square" id="changepw"/>
</div>
</div>
</div>
</div>
<input type="submit" value="Update" id="changepw" name="selection" class="btn btn-primary btn-lg btn-square"/>
</form>
<%}%>
</div>
<div class="footer"></div>
</div>
</body>
</html>
<%if (request.getMethod().equals("POST")) {
if("Update".equals(request.getParameter("selection"))){
try{
employeeDA.updateEmployee(employee);
}catch(Exception ex){
ex.getMessage();
}
}else if("Change Password".equals(request.getParameter("selection"))){
try{
employeeDA.changePassword(username,request.getParameter("password"));
}catch(Exception ex){
ex.getMessage();
}
}
}
out.println(request.getParameter("accType"));
out.println(employee.getAccType());
%>
disabled elements in a form will not be submitted. You may have to use readonly.
I am trying to validate the login and password of the user, in that if not enter the login fields and password will appear a message in the screen "Please fill in the fields of login or password" or if the user type the wrong username or password will appear an "Incorrect Login or Password" screen message but it does not show the messages.
Why does not it show the message on the jsp page?
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String vazio="Por favor preencha os campos de login ou senha";
String incorretos="Login ou senha incorretos";
String login = request.getParameter("login");
String senha = request.getParameter("senha");
Cliente cli= new Cliente();
LoginDAO login2= new LoginDAO();
Cliente login_validacao=login2.validacaoLogin(login,senha);
if(login_validacao.getLogin()==null && login_validacao.getSenha()==null){
request.setAttribute("vazio", vazio);
}
else if(login_validacao.getLogin().equals(request.getParameter("login"))
& login_validacao.getSenha().equals((request.getParameter("senha")))){
RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");
rd.forward(request, response);
}
else{
request.setAttribute("incorreto", incorretos);
}
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="js/jquery-1.12.0.min.js" type="text/javascript"></script>
<script src="js/jquery.maskedinput.js" type="text/javascript"></script>
<script src="js/jquery.maskedinput.min.js" type="text/javascript">
</script>
<script src="bootstrap/bootstrap.min.js" type="text/javascript">
</script>
<script src="bootstrap/bootstrap.js" type="text/javascript"></script>
<link rel="stylesheet" href="bootstrap/bootstrap.css">
<link rel="stylesheet" href="css/css.css" type="text/css">
<link rel="stylesheet" href="css/font-awesome.min.css" type="text/css"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-
awesome.min.css" rel="stylesheet">
<title>JSP Page</title>
</head>
<body>
<div class="login-form">
<div class="row">
<div class="col-6 col-md-4"></div>
<div class="col-6 col-md-4">
<h2 id="AcesseConta"><span>Acesse sua Conta</span></h2>
<label for="vazio_resposta">${vazio}</label>
<label for="validacao">${incorreto}</label>
</div>
</div>
<div class="row">
<div class="col-6 col-md-4"></div>
<div class="col-6 col-md-4">
Login:<input type="text" id="login" name="login" ><br/><br/>
Senha:<input type="text" id="senha" name="senha" ><br/>
Não é cadastrado?Cadastre-se<br/><br/>
</div>
</div>
<div class="row">
<div class="col-6 col-md-4"></div>
<div class="col-6 col-md-4">
<div id="teste"></div>
<button type="button" onclick="login()" >Entrar</button> <button
type="button" onclick="" >Esqueceu a Senha?</button>
</div>
</div>
</div>
</body>
</html>
Instead of validating HTML Form Using Controller/Servlet You Can use 'REQUIRED' tag in html as follow.
Login:<input type="text" id="login" name="login" Required><br/><br/>
Senha:<input type="text" id="senha" name="senha" Required><br/>
And Displaying message on JSP from servlet use below code
for Servlet
if(login_validacao.getLogin()==null && login_validacao.getSenha()==null)
{
response.sendRedirect("login.jsp?vazio=" + URLEncoder.encode(vazio, "UTF-8"));
}
else if(login_validacao.getLogin().equals(request.getParameter("login"))
& login_validacao.getSenha().equals((request.getParameter("senha"))))
{
RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");
rd.forward(request, response);
}
else
{
response.sendRedirect("login.jsp?incorretos=" + URLEncoder.encode(incorretos, "UTF-8"));
}
FOR JSP
<c:if test="${!empty vazio}">
<p style="color:red;" align="center">${param.vazio}</p>
</c:if>
I want to add payments to my clients database but when I add the payments I got this error "GET NOT SUPPORTED" I don't know what's the problem. Can you guys help me?
#Controller
public class PaymentsController {
#Autowired
private UsersService usersService;
#Autowired
private PaymentsService paymentsService;
#RequestMapping(value = "/addPayments", method = RequestMethod.POST)
public String addPayments(HttpServletRequest request, ModelMap map) {
String user = request.getParameter("userId"); // this is the identifier for the user of this payment
String transactName = request.getParameter("transactName");
String paid = request.getParameter("paid");
String unpaid = request.getParameter("unpaid");
String balance = request.getParameter("balance");
String total = request.getParameter("total");
//.... get all other attributes you've passed from the form through request.getParameter("");
//next, check whether or not a user with the userId from the screen exists in the db
Users userObject = usersService.getUsers(user);
//.getUsersByUserId(user);
if (userObject != null) {
// this means that we have a valid user to insert the payment to
UsersPayments payment = new UsersPayments();
payment.setUsers(userObject);
payment.setTransactName(transactName);
payment.setPaid(paid);
payment.setUnpaid(unpaid);
payment.setBalance(balance);
payment.setTotal(total);
//.... set the other properties of UsersPayment object
paymentsService.addPayments(payment);
}
else {
// you have an error right here
}
map.put("paymentsList", paymentsService.getAllPayments());
return "payments";
}
}
payments.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# include file="/WEB-INF/jsp/includes.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="<c:url value="/resources/css/design.css" />" rel="stylesheet">
<link href="<c:url value="/resources/css/customized.css" />" rel="stylesheet">
<link href="<c:url value="/resources/css/bootstrap.css" />" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Canadian Immigration Consultancy</title>
</head>
<body>
<div id="wrapper">
<div id="logo">
<img src="<c:url value="/resources/images/logo4.png" />" height="200px" width="230px"/>
<img src="<c:url value="/resources/images/header06.jpg" />" height="200px" width="765px"/>
</div>
<div class="red">
<div align="center" id="slatenav">
<ul>
<li>Home</li>
<li>View All</li>
<li>Reports</li>
<li>Add Leads</li>
<li><spring:message code="user.logout"/></li>
</ul>
</div>
</div>
<div id="balance" >
<form action="addPayments" method="POST">
<div class="well well-sm box16">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Payments</h3>
</div>
<div class="panel-body">
<div class="form-group">
<br><div class="col-sm-2 control-label">Transaction Name</div>
<div class="col-sm-10">
<input type="text" class="form-control" name="transactName" autofocus />
</div>
</div>
<div class="form-group">
<br><div class="col-sm-2 control-label">Amount Paid</div>
<div class="col-sm-10">
<input type="text" class="form-control" name="paid" />
</div>
</div>
<div class="form-group">
<br><div class="col-sm-2 control-label">Unpaid</div>
<div class="col-sm-10">
<input type="text" class="form-control" name="unpaid" />
</div>
</div>
<div class="form-group">
<br><div class="col-sm-2 control-label">Total Balance</div>
<div class="col-sm-10">
<input type="text" class="form-control" name="balance" />
</div>
</div>
<div class="form-group">
<br><div class="col-sm-2 control-label">Total Amount</div>
<div class="col-sm-10">
<input type="text" class="form-control" name="total" />
</div>
</div>
<div class="save">
<input type="submit" class="btn btn-success" value="Save" />
</div>
</form>
</div>
</div>
</div>
</body>
</html>
You only seem to have one handler mapping, and it's POST. Add another handler mapping for the same url path with GET. Consider the "POST Redirect GET" pattern as well.