setProperty value from requested getParameter - java

I want to give property "host" the value of the requested parameter "ip", submitted using method Get in form.html
This is my index.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ip</title>
</head>
<body>
<jsp:directive.include file="form.html"/>
<% if (request.getParameter("ip") == null ||
request.getParameter("ip") == ""){%>
Not connected!
<% } else {%>
<jsp:useBean id="connect" class="test.Ip" />
<jsp:setProperty name="connect" property="host" value="Connect" />
<jsp:getProperty name="connect" property="host" />
<% }%>
</body>
</html>
This is the included file form.html
<form action="index.jsp" method="GET">
Connect to IP: <input type="text" name="ip"/>
<input type="submit" value="Connect">
</form>
This is the Java file Ip.java
package test;
public class Ip{
private String host;
public String getHost(){
return("Got value " +host);
}
public void setHost(String host){
this.host = host;
}
}
It returns the string "Got value Connect" instead of the value I type in the form.

First of all, to compare strings, you need to use method equals, as you will otherwise compare two object which will not be the same
Change:
request.getParameter("ip") == ""){%
To:
request.getParameter("ip").equals("")){%
then you want to set the property host to the parameter ip
Change:
<jsp:setProperty name="connect" property="host" value="Connect" />
To (As the name attribute of the textfield is ip and not Connect, that's the button):
<jsp:setProperty name="connect" property="host" param="ip" />

Related

How can i get the data of the input hidden field whose name equals to a method and a string in java

Here is a sample jsp page with a form.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="h.abc" %>
<%
abc p = new abc();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="abcd.jsp" method="POST">
<input type="hidden" name="<%=p.getName()%>+'Name'" value='y'>
<input type='text' name='<%=p.getName()%>' >
<input type='submit' value='submit'>
</form>
</body>
</html>
Here is the abc class in the 'h' package.Please pardon the naming.It's only for illustration purposes.
public class abc {
public String name="abc";
public abc()
{
}
public String getName()
{
return name;
}
}
And this is the abcd.jsp target page.Here i am trying to get the value of the input field.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="h.abc" %>
<%
abc p = new abc();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String s = p.getName();
out.println(request.getParameter(s+"Name"));
out.println(request.getParameter(s));
%>
</body>
</html>
I am unable comprehend why the input field is not returning 'y' as its value.rather it is returning null.
change the code in first jsp
<input type="hidden" name="<%=p.getName()%>Name" value='y'>
you need put your abc instance in HttpServletRequest'attributes by calling setAttribute("you_key", abcInstance) method. And then you can get abc instance in your jsp pages by calling req.getAttribute("your_key") or using el expression

Database connectivity in MySQL 5.5

I have written these 2 files:
register. html(which is for users for registering them) and register.jsp(for backend activity)
when the user registers he gets redirected to the server page but after redirection the page is blank show nothing..
Heres the code below for both the files:
`Register(html)
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="http://localhost:9090/RegTrial/sel.jsp">
Rec: <input type="text" name="rec">
<br>
Comp: <input type="text" name="comp">
<br>
<input type="submit" value="submit" name="submit">
<input type="reset" value="clear">
</form>
</body>
Register.jsp
<html>
<%#page import="java.sql.*" language="java"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String rec_no=request.getParameter("rec");
String comp_no=request.getParameter("comp");
Connection con1;
Statement stmt1=null;
ResultSet rs;
String recDb=null,compDb=null;
try
{
out.println("Rec:"+rec_no);
out.println("Comp:"+comp_no);
out.println("*****************************");
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/register";
String user="root";
String password="department";
con1=DriverManager.getConnection(url,user,password);
stmt1=con1.createStatement();
String query="select receipt_no,comp_no from receipt where receipt_no = '"+rec_no+"' and comp_no = '"+comp_no+"'; ";
rs=stmt1.executeQuery("query");
if(rs.next())
{
recDb=rs.getString(1);
compDb=rs.getString(2);
out.println("Rec:"+rec_no);
out.println("RecD:"+recDb);
out.println("Comp:"+comp_no);
out.println("CompD:"+compDb);
if( rec_no.equalsIgnoreCase(recDb) && comp_no.equalsIgnoreCase(compDb))
{
out.println("already registered");
}
else
{
response.sendRedirect("www.google.com");
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>
I have set the environmental variables:
CATALINA_HOME: D:\apache-tomcat-7.0.65
classpath: .;D:\apache-tomcat-7.0.65\lib\servlet-api.jar;D:\apache-tomcat-7.0.65\jar\mysql-connector-java-5.1.37-bin.jar;
path: C:\Program Files (x86)\Java\jdk1.7.0_40\bin;C:\Program Files\MySQL\MySQL Server 5.5\bin
JAVA_HOME: C:\Program Files (x86)\Java\jdk1.7.0_40
(Am using netbeans 7.3,mwsql 5.5,apache tomcat 7.0)

Modifying existing object in one JSP file in another

I want to add another user in registration JSP file to my HashMap which is created in my homepage, but it seems the another HashMap is being created when I'm trying to register user.
How to access the HashMap from homepage JSP file in another?
This is my base class:
package com.jakub.spring;
import java.util.HashMap;
public class registeredUsers {
public HashMap<String, String> userSource;
public registeredUsers() {
userSource=new HashMap<String, String>();
}
public void register(String name, String password) {
userSource.put(name, password);
}
public String userExists(String user) {
String passwordFromSource = userSource.get(user);
if(passwordFromSource != null) {
return passwordFromSource;
}else
return "";
}
}
This is my homepage:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page session="true" %>
<html>
<head>
<title>Home</title>
</head>
<body bgcolor="#CCCFFF">
<form method="post" action="validate.jsp">
<p align="left">Podaj login:</p>
<input type="text" name="name" />
<p align="left">Podaj haslo:</p>
<input type="text" name="password" />
<input type="submit" value="Zaloguj" />
</form>
Rejestracja
<jsp:useBean id="registeredUsers"
class="com.jakub.spring.registeredUsers" scope="application"></jsp:useBean>
<%
out.println("Dostepni uzytkownicy w systemie: \n");
out.print(registeredUsers.userSource.keySet());
%>
</body>
</html>
This is my registration page:
<%# page language="java" contentType="text/html; charset=ISO-8859-2"
pageEncoding="ISO-8859-2"%>
<!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-2">
<title>Konto utworzone</title>
</head>
<body>
<jsp:useBean id="registeredUsers"
class="com.jakub.spring.registeredUsers" scope="application"></jsp:useBean>
<%
registeredUsers.register(request.getParameter("name"),request.getParameter("password"));
out.print("Konto zostało utworzone");
out.print(registeredUsers.userSource.keySet());
%>
Powrót do strony glownej
</body>
</html>
Use ConcurrentHashMap instead of HashMap because it's in Application scope and that can be accessed simultaneously by multiple threads at a time and it results in ConcurrentModificationException.
Please have a look at Thread Safe JSP-Servlet Q&A

HTTP Status 500 - file:/survey.jsp(16,15) jsp:getProperty for bean with name 'survey'. Name was not previously introduced as per JSP.5.3

Hi I am new to JSP and JavaBean. I am practicing by writing an application where multiple pages will share a javabean component. The page "check.jsp" instantiates the bean and sets a property without any error. But whenever I try to getProperty in another jsp, survey.jsp, I get the error:
HTTP Status 500 - file:/survey.jsp(16,15) jsp:getProperty for bean with name 'survey'. Name was not previously introduced as per JSP.5.3
I have double checked that the name in the get and set properties are exactly the same as my bean id in the action element. Please I need help
CHECK.jsp:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<jsp:useBean id="survey" scope="application" class="appScope.SurveyBean"/>
<jsp:setProperty name="survey" property="quantity" value='<%= request.getParameter("title")%>' />
<form action="/appScope/survey.jsp" method="POST">
<h3> Thanks for your input</h3>
<h3> Please check the survey summary status if you want</h3><br/>
<input type="submit" value="Check Status" />
</form>
</body>
</html>
This is my javabean: SurveyBean.java:
package appScope;
public class SurveyBean {
private int javaQuantity = 0;
private int csQuantity = 0;
public int getJavaQuantity()
{
return javaQuantity;
}
public int getCsQuantity()
{
return csQuantity;
}
public void setQuantity(String bookTitle)
{
try
{
if(bookTitle.equals("java"))
{
javaQuantity++;
}
if (bookTitle.equals("c"))
{
csQuantity++;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
and here is survey.jsp where i get the error:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h1>Survey Summary</h1>
//ERROR IS HERE
Java = <jsp:getProperty name="survey" property="javaQuantity" /> <br/>
C# = <jsp:getProperty name="survey" property="csQuantity" />
</body>
</html>
Name was not previously introduced
This indicates that you haven't told your JSP about this bean as of yet. You are directly using the <jsp:getProperty> before letting the JSP know about the bean.
You need to use the <jsp:useBean> tag to define the bean in the survey.jsp.The name attribute of the getProperty tag must match the id attribute of the useBean tag :
<jsp:useBean id="survey" class="appScope.SurveyBean" scope="request">
But this won't work , unless you set the bean named survey in the request scope in the check.jsp file and post that request to the survey.jsp.

MVC - Sending Value of a field with request/session via a JSP

I'm trying to make a list of members of type Party, and linking their memberID to an update page which automatically gets the memberID of the one which was clicked.
I've already written code in the servlet to display a view of all of the members, with each of their IDs linked to a page called UpdateParty.jsp however what I want is for the ID clicked to be passed on with the request so that it can be used in the UpdateParty.jsp as a parameter so that the user does not have to enter it.
I'm using postgres for my SQL if anyone wants to know.
Servlet code which produces a list of all party members:
else if (request.getParameter("listallmembers") != null) {
try {
User sessionuser = (User) session.getAttribute("User");
String u = sessionuser.getUsername();
ArrayList<Party> p = new ArrayList<Party>();
ResultSet rs = this.findAllMembers(u);
while (rs.next()) {
Party party = new Party();
party.setMemberID(rs.getString("memberID"));
party.setPartyFirstname(rs.getString("partyFirstname"));
party.setPartySurname(rs.getString("partySurname"));
party.setUsername(rs.getString("username"));
p.add(party);
}
request.setAttribute("members", p);
request.getRequestDispatcher("ViewPartyMembers.jsp").forward(request, response);
} catch (Exception e) {
out.print(e);
e.printStackTrace(out);
}
Code for ViewPartyMembers.jsp:
<%#page import="HolidayExchange.Party"%>
<%#page import="HolidayExchange.User"%>
<%#page import="java.util.List"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>All Party Members</title>
</head>
<body>
<h1>View Party Members</h1>
<%
List<Party> l = (List<Party>) request.getAttribute("members");
if(l!=null){
out.println("<table>");
out.println("<tr><th>Member ID</th><th>Firstname</th><th>Second Name</th><th>Associated User</th></tr>");
for(int i = 0; i < l.size();i++){
out.println("<tr><td>"+ l.get(i).getMemberID() +
"</td><td><a href='UpdateParty.jsp'>"+ l.get(i).getPartyFirstname() +
"</a></td><td>"+ l.get(i).getPartySurname() +
"</td><td>" + l.get(i).getUsername() + "</td>");
out.println("</tr>");
}
out.println("</table>");
}else{
%>
<form action="PartyServlet" method="get">
<input type="hidden" name="listallmembers" value="1" /><br />
<input type="submit" value="Show all Members" />
</form>
<%
}
%>
</body>
</html>
Here's something a little more sane for you. It uses the JSTL Expression Language and tag library.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>All Party Members</title>
</head>
<body>
<h1>View Party Members</h1>
<c:if test="${!empty members}">
<table>
<tr><th>Member ID</th><th>Firstname</th><th>Second Name</th><th>Associated User</th></tr>
<c:forEach items="${members}" var="member">
<tr><td>${member.memberID}</td>
<c:url value="link" value="UpdateParty.jsp">
<c:param name="memberId" value="${member.memberID}"/>
</c:url>
<td>${member.partyFirstname}</td>
<td>${member.partySurname}</td>
<td>${member.username}</td>
</tr>
</c:forEach>
</table>
</c:if>
<c:if test="${empty members}">
<form action="PartyServlet" method="get">
<input type="hidden" name="listallmembers" value="1" /><br />
<input type="submit" value="Show all Members" />
</form>
</c:if>
</body>
</html>
If you want the ID to be available on the UpdateParty.jsp your link should look like:
<a href='UpdateParty.jsp?id=" + l.get(i).getMemberID() + "'>"+ l.get(i).getPartyFirstname()"</a>
So the id property will be available in the JSP as a request parameter.
Anyway, I recommend you not use scriptlet in your JSP and instead point directly to a JSP point to a Controler/Action

Categories