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}
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 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");
Download the NetBeans project here. File -> Download
Using JSTL 1.2 I'm trying to get my web app to remember my input and then place it into the input box for me after submitting the form but for some reason it doesn't remember it. I just have 1 .java class and .jsp file.
PersonController.java
package controller;
public class PersonController {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# page import="controller.PersonController" %>
<jsp:useBean id="personController" class="controller.PersonController" scope="session"/>
<jsp:setProperty name="personController" property="name" param="name"/>
<!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>PersonController</title>
</head>
<body>
<form method="post" action="index.jsp">
<input name="name" maxlength="30" type="text" id="name" value="<c:out value="${personController.name}"/>"><br/>
<input type="submit" name="button" value="Remember my name">
</form>
</body>
</html>
Errors
HTTP Status 500 - /index.jsp (line: 4, column: 0) The value for the useBean class attribute controller.PersonController is invalid.
If it does not show error #1 then it will not populate the input field name after posting with previous input.
For your first error, I debug your code and you created one parameterized constructor. You need to add default constructor in your PersonController class. It will resolve your "The value for the useBean class attribute controller.PersonController is invalid" error.
For the value being set, You are using Controller and provided doGet method but it has not been defined as Servlet Class, It is a normal java class. You need to extend it with HttpServlet class.
Thanks.
if you only want to remember your input after you submit,there do not need PersonController.java.
just write like this:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# 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=ISO-8859-1">
<title>PersonController</title>
</head>
<body>
<form method="post" action="index.jsp">
<input name="name" maxlength="30" type="text" id="name" value="<c:out value="${param.name}"/>"><br/>
<input type="submit" name="button" value="Remember my name">
</form>
</body>
</html>
This cannot work.
The <jsp:useBean> looks for some bean with name personController in default scope, which is page, and provides it for other parts of your JSP, which is the html input tag in your case. But nobody has set this bean into the pageScope, so the useBean will create new instance of PersonController and put there. The name value is therefore null.
If you don't use any redirects, you can simply fill the input's value according to HTTP request's parameter:
<input name="name" value="<c:out value="${param.name}"/>">
If you are using HTTP redirect (you should after sending POST request), you can store the name into session.
<jsp:useBean id="personController" class="controller.PersonController" scope="session"/>
<jsp:setProperty name="personController" property="name" param="name"/>
The <jsp:setProperty> sets the name property of the previously found (or created) bean personalController to the value of the request parameter with name name.
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.