I am working with a JSP file, trying to work on a simple logon page.
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# page import="com.bridge.service.*" %>
<!-- needs to be run on server to resolve both errors -->
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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">
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.18.custom.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.18.custom.min.js"></script>
<title>Login </title>
</head>
<body>
<form method="POST" name="frmLogon">
User Email: <input type="text" name="email" value="<c:out value="${email}" />" /> <br />
Password: <input type="text" name="password" value="<c:out value="${password}" />" /> <br />
<jsp:useBean id="link" scope="application" class="com.bridge.service.Service" />
<%
Service s = new Service();
String token = s.logonToken(email, password);
%>
<input type="submit" value="Submit" />
</form>
</body>
</html>
I am having a hard time understanding how to take the two values from the POST form and passing them to the Java function. Is this possible? Am I doing this incorrectly?
For simple servlet, you shouod use:
String email = (String)request.getParameter("email");
String password = (String)request.getParameter("password");
Related
I have a problem with JSP Bean's scope - Request. I have a page Index.jsp with jsp bean 'message', its scope is Request and a page result.jsp. When I send request to result.jsp from Index.jsp. My bean 'message' should keep its value but it doesn't now.
I tried with scope Session and my bean worked well. I search all questions about this problem but no answer can meet my question.
Here is my code:
file Index.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
</head>
<body>
<%
String name = request.getParameter("name") == null ? "" :
request.getParameter("name");
int age = ( request.getParameter("age") == null ||
request.getParameter("age") == "") ? 0 :
Integer.parseInt(request.getParameter("age"));
%>
<h1>Nice to meet you</h1>
<form method="post" action="View/result.jsp">
<jsp:useBean id="message" class="com.java.Message" scope="request"/>
<jsp:setProperty name="message" property="message" value="Hello world!"/>
<label>Name: </label> <br>
<input type="text" name="name" placeholder = "Phan Dinh The"/> <br>
<label>Age: </label> <br>
<input type="number" name="age" placeholder = "25"/> <br>
<input type="checkbox" name="title"/> Senior <br>
<input type="radio" name="language" value="c#"/> C# <br>
<input type="radio" name="language" value="java"/> Java <br>
<br><br>
<jsp:include page="View/date.jsp" flush="true"/>
<input type="submit" value="submit"/>
<br><br>
</form>
<br><br>
</body>
</html>
file result.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.java.Message"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="message" class="com.java.Message" scope="request"/>
<jsp:getProperty name="message" property="message"/>
</body>
</html>
my class Message
package com.java;
public class Message {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String content) {
this.message = content;
}
}
I use Tomcat 8.0.23, Jsp version 2.3, Servlet API 3.1
When you use
<jsp:setProperty name="message" property="message" value="Hello world!"/>
in the index.jsp file, that property is scoped to the request of the index.jsp page. once the index jsp page returns to the client, that request is done. When you submit the form, a new request is created, and that is used for the result page generation. Thus when you are in the result.jsp code, there is no request scoped parameter named 'message'.
You could always put the message in an
<input type="hidden" name="message">Hello World</input>
field of the form, and retrieve it in the results.jsp that way.
I was practicing Servlet and JSP and got stuck with an scenario
this is my html page code.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home</title>
</head>
<body>
<form action="Second.jsp" method="post">
<p>Name:<input type="text" name="name"/>
<p>Employee Id:<input type="text" name="empId" />
<p><input type="submit" value="Enter" />
</form>
</body>
</html>
JSP Code
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="person" class="com.Person" scope="request">
<jsp:setProperty name="person" property="name" />
</jsp:useBean>
<jsp:getProperty property="name" name="person"/>
</body>
</html>
Person.java
package com;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
My request goes directly to jsp and the form element name and bean property name matches so no need tp set value in jsp:setProperty
However getProperty is showing null.
You missed the param attribute.
<jsp:useBean id="person" class="com.Person" scope="request">
<jsp:setProperty name="person" property="name" param="name" />
</jsp:useBean>
Within the second page simply use:
${param.name} and ${param.empId}
This is my HTML form :
<form action="supplierportal_home.jsp">
<select id="contract" name="contract">
<option selected="selected">Please Select</option>
<option value="open" >Open</option>
<option value="limited" >Limited</option>
</select>
<input type="text" name="cpv_code" placeholder="<%= cpvOrTenderNo %>">
<button type="submit">FIND <%= contractOrTender %></button>
</form>
And i am getting in JSP
String contract=request.getParameter("contract");
System.out.println("%%%"+contract);
String cpv_code=request.getParameter("cpv_code");
System.out.println("%%%"+cpv_code);
Here is the problem.Seems very nasty.
When I enter both the values then only parameters getting in jsp If I select only the contract from options then null is coming if i give cpv-code then the parameter is coming ...can any one please help to get out this ...
Why my select option values are depending on the other form element cpv-code value,please show some cause for this.
Thanks.
<button type="submit">FIND <%= contractOrTender %></button>
In this example whenever you will enter text in textbox then only contractOrTender
this value can be objtain from textbox.
But as you not entering any value in textbox and trying to access value from dropdown it will come 'null' only. as boz at this time <%= contractOrTender %> this value is null.
which an error in jsp page. thats why you not getting dropdown value even if you dont enter any value in textbox.
Solution
Try to set some default value to textbox. or
change your HTML code.
I have just tested the code and it seems to work fine. No value is depending on the other. Only suggestion is add method="POST" if you don't want the data to be sent over URL.
index.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Index Page</title>
</head>
<body>
<form action="upload.jsp" method="POST">
<select id="contract" name="contract">
<option selected="selected">Please Select</option>
<option value="open" >Open</option>
<option value="limited" >Limited</option>
</select>
<input type="text" name="cpv_code" />
<button type="submit">FIND</button>
</form>
</body>
</html>
upload.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Upload JSP</title>
</head>
<body>
<%
String contract=request.getParameter("contract");
System.out.println("%%%"+contract);
String cpv_code=request.getParameter("cpv_code");
System.out.println("%%%"+cpv_code);
%>
</body>
</html>
I am tring to run function that get 2 variables one of them should get the value from textfield.
any advice?
<%#page import="root.SQLQuery"%>
<%# page language="java" contentType="text/html; charset=windows-1255"
pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%# page import="root.SQLQuery"%>
<%
String emp_email = session.getAttribute("email").toString();
SQLQuery sql = new SQLQuery();
String link="";
%>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<title>Insert title here</title>
</head>
<body>
<center>
<table width="900" height="900">
<iframe src="http://www.aol.com/av" width="900" height="900"></iframe>
<center>
<font color=red>your email is:<%=emp_email%>></font>
Copy the link here: <input type="text" name="videoLink" id="link">
<input type="submit" value="submit" onclick="<%sql.meetingUpdateVideoLink(emp_email, request.getParameter("videoLink").toString()); %>>">
</center>
</table>
</center>
I am tring to insert the fanction meetingUpdateVideoLink() two variables:
email - that easy I get from session.
Link - that is the tricky one I need to get it from the textfield name="videoLink"
Thanks,
Cfir
Hy!
I have a jsp site called konto (engl. account)
At the end i have a button that should invalidate the current session by clicking and redirect back to the loginpage but that doesn't work.
Code:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="konto" scope="session" class="at.korn.web.Konto"></jsp:useBean>
<% if (session.getAttribute("user")== null)
{
%>
<jsp:forward page="index.jsp"></jsp:forward>
<% }
if (request.getParameter("logout")!= null)
{
session.invalidate();
%>
<jsp:forward page="index.jsp"></jsp:forward>
<% } %>
<% konto.holeKontostand(String.valueOf( session.getAttribute("user")));%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Konto</title>
</head>
<body>
<h1>Kontoübersicht</h1>
<p>Herzlich Willkommen <% out.print(session.getAttribute("user"));%> </p>
<p>Ihr Kontostand beträgt: ${konto.ktostand} </p>
<input type="submit" value="Logout" name="logout" />
<br>
</body>
</html>
The Error should be in that file.
Please help!
You need to put the button in a <form> in order to get it to work.
<form>
<input type="submit" value="Logout" name="logout" />
</form>
That said, mingling model, view and controller in a single JSP (view) isn't the best practice.