I have a problem, I'm not sure how to save selected option to a file.
What the code looks like is:
<%# page import="java.io.PrintWriter" %>
<%# page import="java.time.LocalDateTime" %>
<%# 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>Combo box to file</title>
</head>
<body>
<%PrintWriter writer; %>
<select>
<option value="Date">
Current Date and Time
<%
writer = new PrintWriter("text.txt", "UTF-8");
writer.println(LocalDateTime.now().toString());
writer.close();
%>
</option>
<option value="Text">
Some Text
<%
writer = new PrintWriter("text.txt", "UTF-8");
writer.println("Some text");
writer.close();
%>
</option>
<option value="Integer">
An Integer
<%
writer = new PrintWriter("text.txt", "UTF-8");
writer.println(123);
writer.close();
%>
</option>
</select>
</body>
</html>
And it doesn't do anything. My guess is that the code isn't invoked but I'm very new to this, not sure how to solve this.
Related
I have Spring controller and I am setting a model value and want to display in my JSP:
public class TestController {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(Model model){
model.addAttribute("message", "Programmer Gate");
return "/test";
}
}
My test.jsp code:
<%# 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>
<title>Welcome</title>
</head>
<body>
<%
double num = Math.random();
if (num > 0.95) {
%>
<h2>You'll have a luck day!</h2><p>(<%= num %>)</p>
<%
} else {
%>
<h2>Well, life goes on ... </h2><p>(<%= num %>)</p>
<%
}
%>
<p>${message}</p>
</body>
</html>
I am not seeing the message value as the JSP rendering as follows:
Well, life goes on ...
(0.3343913194169942)
${message}
How to access the model message variable value in the JSP?
I have the following:
select name="nroPartido" style="color:#F5FFFA; background-color: #CC9900; font-weight: bold;">
<%
//se crean las listas
java.util.ArrayList<Partido> lista = Pronosticos.getInstance().getMiLista();
int nro = 0;
for (Partido p : lista) {
out.println("<option value=\"" + nro + "\">" + p.getLocal() +"-" +p.getVisitante() + "</option>");
nro++;
}
%>
</select>
So when I click the button the value of nro will be the value of the var nroPartido that is in the pronosticoAction class:
package acciones;
import com.opensymphony.xwork2.ActionSupport;
public class pronosticoAction extends ActionSupport {
private int nroPartido;
public String execute() {
System.out.println(nroPartido);
return SUCCESS;
}
public int getNroPartido() {
return nroPartido;
}
public void setNroPartido(int nroPartido) {
this.nroPartido = nroPartido;
}
}
Then what i want to do is print that number in a JSP page. So i do the following:
<%# 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>Detalles partido</title>
</head>
<body>
<h1>Chosen number</h1>
<h4>
You select number: <s:property value="nroPartido" />
</h4>
</body>
</html>
The problem is that it only shows this:
If someone could help me will be very useful
Thanks!
The following line is wrong, both syntactically and conceptually:
<s:property value="nroPartido"></<s:property>
there is an extra < , and the <s:property/> tag should self-closed, like a void element in XHTML:
<s:property value="nroPartido" />
That said, you should consider building your Select without using scritplets at all, by either iterating the options with <s:iterator>, or by using <s:select/> that is often the right way. You can find an example on how to do it in this answer.
EDIT
You also forgot to include the taglib directive for Struts2 tags:
<%# taglib prefix="s" uri="/struts-tags" %>
To use the Struts 2 tags on the view page, you must include a tag library directive. Typically, the taglib directive is <%# taglib prefix="s" uri="/struts-tags" %>. So the prefix for all the Struts 2 tags will be "s".
If you want to actually read the Struts 2 tag TLD file, you'll find it in the META-INF folder of the Struts 2 core jar.
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
I using a JSP page to write into a file
Input Box in JSP will send some DATA
DATA
FARSI (Persian): INPUT_MSG = کد من کار نمی کند برای زبان فارسی.
<notification><myprojectid>256333859</myprojectid><mobile>123456789</mobile><uniqueid>ABCDEF565667DD</uniqueid><noation/><title>Push-Notification</title><message>INPUT_MSG</message><timestamp>20132611112245</timestamp></notification>
But in the file it has been stored in like given value
éï ÃÂ
àéçñ ÃÂÃÂ
à éÃÂï èñçà òèçàÃÂçñóÃ.
JSP PAGE Encoding For : SendMessage.jsp
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<body>
<form method="post" action="genMsgFile.jsp" name="testForm">
<span>Farsi Message : </span>
<input type="text" name="faMSg" />
<input type="submit" value="Push Message" />
</form>
</body>
</html>
JSP PAGE Encoding For : writeInfile.jsp
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%#page import="java.sql.*"%>
<%#page import="java.io.*"%>
<%#page import="java.util.*"%>
<%#page import="java.lang.*"%>
<html>
<body>
<%!
public void createFile(String msgLocation, String filepath, String msgTosend){
String fp = msgLocation + filepath;
// System.out.println("$$ PATH $$$ "+ fp);
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(fp));
pw.println(msgTosend);
pw.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
%>
<%
String farMsg = request.getParameter("faMSg");
createFile(msgPATH,farsiFile,farMsg);
%>
</body>
</html>
What changes i should make in writeInfile.jsp so it will stored in readable format ???
I have the class A:
package a;
public class A {
private int x = 9;
public int getX() {
return x;
}
}
and the ajsp.jsp file:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/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>
<jsp:useBean id = "a" class = "a.A" />
<c:out value = "${a.x}" />
</body>
</html>
when i run it, it gives an error :
org.apache.jasper.JasperException: /ajsp.jsp(11,0) PWC6236: According to TLD or attribute directive in tag file, attribute value does not accept any expressions
if instead of <c:out value = "${a.x}" /> i use <jsp:getProperty property="x" name="a"/> it goes perfect.
So, what is the problem?
Thank advance.
Your taglib URI is incorrect, you're using the URI of the old pre-expression, pre-JSP 2.0 library.
Instead of
http://java.sun.com/jstl/core
it should be
http://java.sun.com/jsp/jstl/core