Adding Parameters to Hello World JSP page - java

I'm new to this so i'm sure there is a simple solution but the question I'm trying to answer is this "This page should display a simple hello world message. In addition, your JSP page should look for a parameter in the request (GET) named “user”. If the user parameter is present, it should read the name from the request and display a greeting to the user. If the parameter is not present, it should display a generic “Hello World” message"
So far what I have is this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World - JSP tutorial</title>
</head>
<body>
<%= "Hello World!" %>
</body>
</html>
How would I go about adding the parameters?

Here it goes:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World - JSP tutorial</title>
</head>
<body>
<%
String user = request.getParameter("user");
response.getWriter().println(user == null ? ("Hello World") : ("Hello World " + user));
%>
</body>
</html>

Related

"/showForm" does not redirect to the page showForm.jsp but "showForm" does it

index.jsp
<!DOCTYPE html>
<html>
<head>
<title> Home Page|Spring MVC </title>
</head>
<body>
<h2>Welcome to spring mvc</h2>
Form
</body>
</html>
Below is my controller code
#RequestMapping("/showForm")
public String showForm() {
return "showForm.jsp";
}
Here is the jsp code I am using to render the showForm.jsp page, the problem is that whenever I am using /showForm it gives 404 and when I use showForm it render the showForm.jsp page.
You just have to return name of the .jsp page from the endpoint and also you have to append context path of the request with URI.
Modify to this :
index.jsp
<!DOCTYPE html>
<html>
<head>
<title> Home Page|Spring MVC </title>
</head>
<body>
<h2>Welcome to spring mvc</h2>
Form
</body>
</html>
Controller :
#RequestMapping("/showForm")
public String showForm() {
return "showForm";
}
It should work.

How do you show the content of a session in a JSP for java?

i try to get the information of a user on my database, store the object in a session then show the session's contents on a JSP page. Whenever I try to show it on my page, the content of the session does not appear. Please help.
THIS IS THE JSP PAGE I WAS TALKING ABOUT:
<%#page import="neospa.Client"%>
<%#page import="DAOs.ClientDAO"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>CLIENT INFORMATION</title>
</head>
<body>
<h1>CLIENT INFORMATION</h1>
<br>
<%Client client = (Client)session.getAttribute("Client");%>
First Name <% client.getFname(); %> Last Name <% client.getLname();%>
<br>
Birthday <%client.getBirthday();%>
<br>
Home Number <%client.getHomeno();%>
<br>
Mobile Number <%client.getMobileno();%>
<br>
Office Number <%client.getOfficeno();%>
<br>
Email Address <%client.getEmail();%>
<br>
Address: <%client.getAddress();%>,<%client.getCity();%>,<%client.getRegion();%>,<%client.getCountry();%>
</body>
</html>
Is there something missing here that I need to put? :)

JSP Encoder Issue While sending Russian character in QueryString

I have two jsp pages. I am trying to add "Russian" language. Russian characters are shown perfectly on jsp page, but when I try to send this value to another jsp page from parameter then in second jsp page this value is changed to different characters. This problem is only in Russian Language and not in others such as Italy and French.
For example
On demo.jsp page the russian character "приветствие" is shown correctly.
but when I try to send it to another page "test.jsp" then some unknown
characters are shown like "!C<Cä5 Cô>CôCC´OD=Cä5!"
Code:
demo.jsp
String welcometext=langP.get("welcome");
<jsp:include page="<%=test.jsp%>">
<jsp:param name="wlc" value="<%=Encode.hex8Code(welcometext)%>" />
</jsp:include>
In test.jsp
String title = Encode.utfToUnicode(Decode.hex8Decode(request.getParameter("wlc")));
System.out.println(" Russial welcome test "+welcome);
Is there any special code we need to add for Russia while sending them in query parameters??
Please note* the following code are already written else it would have given problem for French and Italy language too..
<%# page contentType="text/html; charset=UTF-8" %>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
Also tried with following but didn't help out!
request.setCharacterEncoding("UTF-8")
Try to add <% request.setCharacterEncoding("UTF-8"); %> to your main jsp page:
Here is my example:
demo.jsp
<%#page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Привет</h1>
<jsp:include page="/test.jsp" flush="true">
<jsp:param name="wlc" value="Привет"/>
</jsp:include>
</body>
</html>
test.jsp
<h1>Param values is</h1>
<%
String hello = request.getParameter("wlc");
out.print(hello);
%>
I don't know the better solution but the following code solved this issue. I kept the variable in session attribute.
demo.jsp
session.setAttribute("welcometext", welcometext);
test.jsp
String welcometest=(String) session.getAttribute("welcometext");

date does not get printed : why is this so?

In the javascript function jsp I am trying to print the date.But it doesn't get printed. Why is this so ? The date should get printed before the text in the h1 tag. But the problem is date doesn't get printed ! Why is this so ?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP - 1</title>
<script>
function jsp() {
<%= new java.util.GregorianCalendar().getTime().toString() %>
}
</script>
</head>
<body>
<h1>
Was I printed first ? Or is it the date... ..
</h1>
<script type="text/javascript">
setTimeout(jsp,2000);
</script>
</body>
<script>
function jsp() {
document.write('<%= new java.util.GregorianCalendar().getTime().toString() %>');
// or any other JS function you may want to use
}
</script>
You're mixing server-side and client-side.
With your original function, your browser will see (for example)
<script>
function jsp() {
2012-08-24 11:57:00
}
</script>
but this isn't JS-valid (as you see).
And to answer your hidden question, the date will be printed last, because it's located after the h1 (in a DOM-speaking way).

Open a new browser window in GWT containing a Widget/Panel

I know you can open a new browser window using the Window.open() function directing it to a specific URL. However, is it possible to open a new browser Window containing a GWT Panel? And if it is, can someone show an example of it?
Here's my idea. I implemented it in pure JavaScript, but if it's possible in JS, it should also be possible with GWT!
parent.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Parent page</title>
<script type="text/javascript">
function openChild() {
window.mychild = window.open("print.html");
setTimeout('setContent();', 2000);
}
function setContent() {
window.mychild.document.body.innerHTML =
'<b>Here is your dynamically generated print content</b>';
// This could be produced by your main GWT module.
}
</script>
</head>
<body>
Open child window
</body>
</html>
print.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Print view</title>
</head>
<body>
One moment please...
</body>
</html>
Note, that I used a timeout. This isn't ideal, because if the (very small) print.html takes longer to be fetched, then it will overwrite the dynamically set content.
I assume, the solution could be optimized (but make sure that the child window is fetched from the same origin, otherwise you'll run into "Same Origin Policy" problems).

Categories