Why request.getParamether don't work in jsp - java

Hello i can't get any value from a from in my index.jsp file. I'm using tomcat7, after running index.jsp and clicking send button nothing happen. System.out.prinln() print nothinig or null ;(
index.jsp
%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<form acton="addnewtask.jsp" method="post" >
<label for="name">Name</label>
<input type="text" class="form-control" id="name" placeholder="Name">
<button type="submit" class="btn btn-danger">Add</button>
</body>
</html>
addnewtask.jsp
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%
String s = request.getParameter("name");
System.out.println(s);
%>
Do you know what am i doing wrong ?

This line allows you to get a parameter with its name not its id :
String s = request.getParameter("name");
Add a name to your input and correct the typo of the attribute action of your form :
<form action="addnewtask.jsp" method="post" >
<label for="name">Name</label>
<input name="name" type="text" class="form-control" id="name" placeholder="Name">
<button type="submit" class="btn btn-danger">Add</button>
<form>

String s = request.getParameter("xyz");
Add a name to your input box
<form action="addnewtask.jsp" method="post" >
<label for="name">Name</label>
<input type="text" class="form-control" name="xyz" id="name" placeholder="Name">
<button type="submit" class="btn btn-danger">Add</button>
<form>

Related

In my spring web mvc, suddenly jsp expression such as ${message} is not displaying even I have the embedded-jasper and tomcat

It was working fine but suddenly its now working for ${message} and others as ${param.someName}
this is my .java class
#PostMapping("/validate")
//All the libraries are also imported and no error in code
String validateLogin(String mail,String password,String location,HttpSession session,HttpServletRequest req,Model model) {
User user;
//user=urepo.findByEmail(mail);
user=urepo.findByEmailAndPassword(mail,password);
if(user!=null)
{
req.getSession();
session.setAttribute("userid", user);
System.out.println(location);
System.out.println(user.getFname());
return "redirect:/"+location;//+"?session=userid";
}
model.addAttribute("message", "please check the username/password");
//req.setAttribute("message", "Please check the username or password");
return "redirect:login?location="+location;
}
below is my .jsp login form when I'm entering wrong password on the jsp page I want to display the message which is written in the above java file but its not displaying the ${message} and also not working with the ${param.email} too but login is working fine
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
//Bootstrap link
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
</head>
<body>
<div class="container-md text-center">
<div class="row mx-auto pt-5">
<h1>Welcome to Ekart...</h1>
<h3><i>Login please!!</i></h3>
<form action="validate" method="post">
<div class="col-md-5 col-sm mx-auto pt-2 pb-2">
<span>${message}</span>
UserName: <input type="email" name="mail" placeholder="email" value="${param.mail}">
</div>
<input type="hidden" name="location" value="${param.location}">
<!-- ${param.location} -->
<div>
<div class="col-md-5 col-sm mx-auto pt-2 pb-2">
Password: <input type="password" name="password" placeholder="password">
<br>
</div>
<input type="submit" class="btn btn-primary" value="login">
</form>
<br><br>
Create a Account?
</div>
</div>
</form>
</div>
</body>
</html>

How to auto update a form textfield from a uploaded .csv file using JSP?

I've created a JSP file for creating a form.
I'm trying to auto update or print data automatically into a text field in a form by uploading a .csv file using input type=file tag and then calculate the total price of items from the .csv file using JSP. That is I've to print the total price calculated from the .csv file onto the text field of the form.
Below is the JSP file.
Say the .csv file contains only 1 column containing prices of different items.
<%# 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>Insert title here</title>
<link rel="stylesheet" href="../CSS/bootstrap.css">
</head>
<body>
<div class="row">
<div class="container">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="form-area">
<form role="form" method="post" action="../AddDistributorDetail"> <br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Distributor Registration</h3>
<div class="form-group">
<label class="control-label">Distributor Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<label class="control-label">Contact No #1:</label>
<input type="text" class="form-control" id="contact1" name="contact1" placeholder="Contact No1" required>
</div>
<div class="form-group">
<label class="control-label">Contact No #2:</label>
<input type="text" class="form-control" id="contact2" name="contact2" placeholder="Contact No2" required>
</div>
<div class="form-group">
<label class="control-label">Address #1:</label>
<textarea class="form-control" type="textarea" id="address1" placeholder="First Address" maxlength="200" rows="7"></textarea>
</div>
<div class="form-group">
<label class="control-label">Address #1:</label>
<textarea class="form-control" type="textarea" id="address2" placeholder="Second Address" maxlength="200" rows="7"></textarea>
</div>
<div class="form-group">
<label class="control-label">City:</label>
<input type="text" class="form-control" id="city" name="city" placeholder="City" required>
</div>
<div class="form-group">
<label class="control-label">Pincode:</label>
<input type="text" class="form-control" id="pin" name="pin" placeholder="Pincode" required>
</div>
<div class="form-group">
<label class="control-label">Zone:</label>
<div class="radio">
<label><input type="radio" name="zone">East</label>
</div>
<div class="radio">
<label><input type="radio" name="zone">South</label>
</div>
<div class="radio">
<label><input type="radio" name="zone">West</label>
</div>
<div class="radio">
<label><input type="radio" name="zone">North</label>
</div>
</div>
<div class="form-group">
<label class="control-label">STB Limit:</label>
<input type="text" class="form-control" id="stblimit" name="stblimit" placeholder="Set Top Box Limit" required>
</div>
<div class="form-group">
<label class="control-label">Credit Limit:</label>
<input type="text" class="form-control" id="climit" name="climit" placeholder="Max Credit Limit" required>
</div>
<div class="form-group">
<label class="control-label">Upload STB Inventory</label>
<input type="file" class=" file form-control" id="file_upload" name="file_upload" enctype="multipart/form-data" multiple required>
</div>
<div class="form-group">
<label class="control-label">Total Cost Of Inventory</label>
<input type="text" class="form-control" id="tcost" name="tcost" placeholder="Total Cost Of Inventory" required>
</div>
<button type="button" id="submit" name="submit" class="btn btn-primary">Submit Form</button>
</form>
</div>
</div>
<div class="col-md-2"></div>
</div>
</div>
<script type="text/javascript" src="../JS/jquery.js"></script>
<script type="text/javascript" src="../JS/bootstrap.js"></script>
</body>
</html>
Even though I understood as how to read a csv file in JSP, I searched the internet but didn't find any logic as how to upload the file and calculate the total price in real time(i.e in the same JSP page and before clicking the submit button) and print it the text field in the form.
Any help regarding this will be much appreciated.
You can upload the file using ajax form submit. After uploading, in the server side just read the csv and writes the result back in the response. After receiving the result you can manipulate the dom element.
The easier way is to use the javascript "FileReader" api, which will be supported in most browser
<script type='text/javascript'>
var readFile = function(event) {
var input = event.target;
var reader = new FileReader();
reader.onload = function(){
document.getElementById("myTextarea").value =reader.result;
};
reader.readAsText(input.files[0]);
};
</script>
<body>
<input type='file' onchange='readFile(event)'><br>
<textarea id='myTextarea'></textarea>
</body>
Js fiddle link : http://jsfiddle.net/gtxfye8m/
I've tested in chrome, edge.
You can create a Pojo class and declare all the fields which are present in the file and read the fields from your csv file and then set it into the respective objects that can be mapped to your jsp fields.

JSP exception quote symbol expected

I am new to spring MVC, JSP and bootstrap. I am trying to create a sign in form and read the values from it. For this purpose the login.jsp file I wrote is;
<%# page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%#taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%#taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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>Bootstrap Form With Spring Mvc Example</title>
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css"
rel="stylesheet">
<link href="https://code.jquery.com/jquery-2.1.3.js" rel="stylesheet">
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<div class="container">
<form:form class="form-signin" method="POST" commandName="user">
<h2 class="form-signin-heading">
<spring:message code="main.signin" />
</h2>
<label for="email" class="sr-only">
<spring:message code="main.email" />
</label>
<form:input type="email" path="email" id="email" class="form-control" placeholder=<spring:message code="main.email"/> required autofocus/>
<label for="password" class="sr-only">
<spring:message code="main.password" />
</label>
<form:input type="password" path="password" id="password" class="form-control" placeholder=<spring:message code="main.password"/> required/>
<div class="checkbox">
<label> <input type="checkbox" id="rememberme">
<spring:message code="main.rememberMe" />
</label>
</div>
<input class="btn btn-lg btn-primary btn-block" type="submit" id="submit" value=<spring:message code="main.signinBtn" /> >
</form:form>
</div>
<jsp:include page="footer.jsp"></jsp:include>
</body>
</html>
But I get
SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/SpringMVC] threw exception [/WEB-INF/pages/login.jsp (line: 26, column: 86) quote symbol expected] with root cause
org.apache.jasper.JasperException: /WEB-INF/pages/login.jsp (line: 26, column: 86) quote symbol expected
exception. The line I get this exception is
<form:input type="email" path="email" id="email" class="form-control" placeholder=<spring:message code="main.email"/> required autofocus/>
I don't have any idea about the problem. Can you help?
You can't use <spring:message> within <form:input> in the manner attempted. Look at this question for the solution: how-to-use-springmessage-inside-an-attribute-of-forminput

How to fully convert JSP scriptlet into JSTL?

I am new to JSTL, could someone please tell me how to convert below JSP and HTML code to full JSTL with no scriptlet in the page?
I'd also be grateful for suggestions about some good resources to learn JSTL and advanced JSP concepts such as JSF and spring with CRUD example.
This is curd example taken from http://javaknowledge.info/?p=478. I took this example because I thought it is 100% JSTL implementation but I was wrong.
My IDE is NetBeans.
<form method="POST" action='UserController' name="frmAddUser">
<% String action = request.getParameter("action");
System.out.println(action);
%>
<% if (action.equalsIgnoreCase("edit")) {%>
User Name : <input type="text" name="uname"
value="<c:out value="${user.uname}" />" readonly="readonly"/> (You Can't Change this)<br />
<%} else {%>
User Name : <input type="text" name="uname"
value="<c:out value="${user.uname}" />" /> <br />
<%}%>
Password : <input
type="password" name="pass"
value="<c:out value="${user.password}" />" /> <br />
Email : <input
type="text" name="email"
value="<c:out value="${user.email}" />" /> <br />
<% if (action.equalsIgnoreCase("edit")) {%>
Registration : <input
type="text" name="dob"
value="<fmt:formatDate pattern="yyyy/MM/dd" value="${user.registeredon}" />" readonly="readonly"/>(You Can't Change this) <br />
<%} else {%>
Registration : <input
type="text" name="dob"
value="<fmt:formatDate pattern="yyyy/MM/dd" value="${user.registeredon}" />" />(yyyy/MM/dd) <br />
<%}%>
<input type="submit" value="Submit" />
</form>
There are only two types of scriptles:
print the current action into system's out - there is no JSTL alternative
if-else statement can be rewritten using
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:choose>
<c:when test='${ fn:toLowerCase(param.action) eq "edit" }'>
...
</c:when>
<c:otherwise>
...
</c:otherwise>
</c:choose>
Years ago i learnd alot from this videos:
https://www.youtube.com/user/3n3xus/videos
Too bad, its in German and eclipse.
You learn
how to write your own JSTL-Framework.
how to write and navigate in jsf
work with richfaces.
how to work with facelets.

How can I go back from a JSP page to index.html (the login page) ?

I'm starting from here :
With that code :
<!DOCTYPE html>
<html>
<head><title>Bank application</title>
<link rel="stylesheet"
href="./css/styles.css"
type="text/css"/>
</head>
<body>
<table class="title">
<tr><th>Web Bank application</th></tr>
</table>
<br/>
<fieldset>
<legend>Login Page - please enter your Username and Password</legend>
<form action="loginPage">
Username: <input type="text" name="username"><br>
Password : <input type="text" name="password"><br>
<input type="submit" value="Login">
</form>
</fieldset>
<br/>
<br/>
<br/>
<fieldset>
<legend>Registration</legend>
<form action="register">
First name: <input type="text" name="firstName"><br>
Last name : <input type="text" name="lastName"><br>
Address : <input type="text" name="address"><br>
ID-number : <input type="text" name="idnumber"><br>
User-Name : <input type="text" name="userName"><br>
Password : <input type="text" name="password"><br>
<input type="submit" value="Register">
</form>
</fieldset>
<br/>
<br/><br/><br/><br/><br/><br/>
</body></html>
And while I move from one page to another , I reach here :
With that code :
<!DOCTYPE html>
<html>
<head><title>Authentication failed - a problem has occurred!</title>
<link rel="stylesheet"
href="./css/styles.css"
type="text/css"/>
</head>
<body>
<h1>Sorry , but you are not registered to our bank!</h1>
<fieldset>
<legend>Please press here to continue</legend>
<form action="goingBack">
<input type="submit" value="Press here">
</form>
</fieldset>
</body></html>
And I want to go back to index.html - the first page that I see when the program starts (the first picture above) .
How can I do that ? how can I forward back to index.html ?
Regards
Just add a link to the previous page in the last HTML:
<!DOCTYPE html>
<html>
<head><title>Authentication failed - a problem has occurred!</title>
<link rel="stylesheet"
href="./css/styles.css"
type="text/css"/>
</head>
<body>
<h1>Sorry , but you are not registered to our bank!</h1>
<fieldset>
<legend>Please press here to continue</legend>
<form action="goingBack">
<input type="submit" value="Press here">
</form>
</fieldset>
Go Back <!-- add this -->
</body></html>
You just need javascript. To redirect to /index.html after 5 seconds :
setTimeout('window.location='index.html';', 5000);
setTimeout() is a javascript function that sets a timer to trigger somehting after a given period of time. window.location is a variable that allows you to change the URL of the current page (thus redireting).

Categories