reading and writing Persion Language(FARSI) Data - java

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 ???

Related

Unable to format date properly

I have an issue with formatting the date.
In my addstudent.jsp file I am asking user ti enter their date of birth. This I am doing using input type="text".Add the format to enter is dd-mm-yyyy. Now I have to store the date in yyyy-mm-dd format as mysql stores in yyyy-mm-dd format.
I did this in addprocess.jsp file. But something is wrong which I do not understand. When I give 08-05-2009 as input, I am getting 2009-01-08
as output to store in mysql database. What am I doing wrong?
This is my addstudent.jsp file.
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Adding Student</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
function validateform(){
var errorString="";
var name=document.myform.name.value;
var email=document.myform.email.value;
var parentemail=document.myform.parentemail.value;
if (name==null || name=="" || !(/^[A-Za-z\s]+$/.test(name))){
document.getElementById("error").innerHTML=errorString+"Name can have only alphabets and spaces";
return false;
}
function validateEmail(email){
var re = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)| (".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
if(!validateEmail(email)){
document.getElementById("error").innerHTML=errorString+" Your email is not valid";
return false;
}
if(!validateEmail(parentemail)){
document.getElementById("error").innerHTML=errorString+" Your parent's email is not valid";
return false;
}
}
</script>
</head>
<body>
<div id="outer">
<%#include file="header.jsp" %>
<%# page import="java.util.*"%>
<%# page import="java.util.regex.*"%>
<%#page import="java.sql.*"%>
<div id="error"></div>
<div id="main">
<div id="box">
<form name="myform" method="post" action="addprocess.jsp" onsubmit="return validateform();">
<table>
<tr><td style="color:navy;"><B> Login Form</B></td> </tr>
<tr><td><br></td></tr>
<tr><td>User Name:</td><td><input type="text" name="name" placeholder="Enter your name"/></td></tr>
<tr><td><br></td></tr>
<tr><td>Email Id:</td><td><input type="text" name="email" placeholder="Enter Email ID"/></td></tr>
<tr><td><br></td></tr>
<tr><td>Parent Email Id:</td><td><input type="text" name="parentemail" placeholder="Enter Parent's Email ID"/></td></tr>
<tr><td><br></td></tr>
<tr><td>Date Of Birth:</td><td><input type="text" name="dateofbirth" placeholder="dd-mm-yyyy"/></td></tr>
<tr><td><br></td></tr>
<tr><td>Address:</td><td><input type="text" name="address" placeholder="Enter your address"/></td></tr>
<tr><td><br></td></tr>
<tr><td> </td><td><input type="submit" value="Sign in"></td></tr>
</table>
</form>
<br>
</div>
</div>
</div>
</body>
</html>
And this is addprocess.jsp file
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%# page import="java.util.*"%>
<%# page import="java.util.regex.*"%>
<%#page import="java.sql.*"%>
<%#page import="java.text.ParseException"%>
<%#page import="java.text.SimpleDateFormat"%>
<%#page import="java.text.DateFormat"%>
<%#page import="java.util.Date"%>
%>
<%
String name=request.getParameter("name");
String email=request.getParameter("email");
String parentemail=request.getParameter("parentemail");
SimpleDateFormat format = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date util_Date = format.parse( request.getParameter("dateofbirth") );
java.sql.Date dateOfBirth = new java.sql.Date( util_Date.getTime() );
System.out.println(dateOfBirth);
String address=request.getParameter("address");
%>
</body>
</html>
Thanks in advance.
"mm" in date formatting is minute; "MM" in date formatting is month.

jsp and getting attributes data

I am trying to display the note attribute that I saved into my request.setAttribute("entries", entries), but I can't access this data in my jsp and I can't figure it out. I've tried {note} , but that doesn't work.
This is my controller class:
while( rs.next() )
{
int id = rs.getInt("id");
String name = rs.getString("name");
String note = rs.getString("note");
String title = rs.getString("title");
Notes entry = new Notes(id, name, note, title);
entries.add(entry);
}
request.setAttribute("entries", entries);
request.getRequestDispatcher( "/WEB-INF/homework2/MyNotes.jsp" ).forward(
request, response );
}
catch( SQLException e )
{
throw new ServletException( e );
}
finally
{
try
{
if( c != null ) c.close();
}
catch( SQLException e )
{
throw new ServletException( e );
}
}
}
}
This is my jsp view:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%# 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" %>
<%# taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>MyNotes</title>
</head>
<body>
<p align="right">Hello, ${sessionScope.CurrentUser}! Logout</p>
<span>JOT My Notes | New</span>
<br>
<br>
<br>
<p>${note} </p>
<p>${applicationScope.entries.note},</p>
</body>
</html>
You already have your List in your request so:
Get the list with EL language
Iterate it to get all objects inside.
Then you can, (for example) put info a table:
<c:forEach items="${requestScope.entries}" var="entry">
<tr>
<td>ID: <c:out value="${entry.id}"/></td>
<td>Name: <c:out value="${entry.name}"/></td>
<td>Note: <c:out value="${entry.note}"/></td>
<td>Title: <c:out value="${entry.title}"/></td>
</tr>
</c:forEach>

Using jquery to show time every 3 seconds does not work properly

I am very new at "jsp" and "jquery", I think the code below should display a number on screen and increase it by one every 3 seconds, but after 2 or 3 repetitions it breaks and starts to show wrong numbers
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.11.2.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function () {
$('#load_me').load('index.jsp').fadeIn("fast");
}, 3000); // autorefresh the content of the div after
//every 3000 milliseconds(3sec)
</script>
</head>
<body>
<%! int i = 0;%>
<div id="load_me">
<%out.print(++i);%>
</div>
</body>
</html>
I even tried to show time instead of printing a number, but the same problem accrued :
<%# page import="java.text.SimpleDateFormat" %>
<%# page import="java.util.Date" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.11.2.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function () {
$('#load_me').load('index.jsp').fadeIn("fast");
}, 3000); // autorefresh the content of the div after
//every 3000 milliseconds(3sec)
</script>
</head>
<body>
<div id="load_me">
<%
Date d = new Date();
SimpleDateFormat sp = new SimpleDateFormat("hh:mm:ss");
String t= sp.format(d);
out.print(t);
%>
</div>
</body>
</html>
it seems you want a 'div#load_me' should display a number increment of 1 every 3 seconds.. Try the following plain javaScript for the same:
setInterval((function() {
var currNumber = 0;
return function() {
document.getElementById('load_me').innerHTML = ++currNumber;
}
})(), 3000);
EDIT (To demonstrate the full code) :
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<!-- we do not need jquery any more :-) -->
</head>
<body>
<div id="load_me">
</div>
<script>
setInterval((function() {
var currNumber = 0;
return function() {
document.getElementById('load_me').innerHTML = ++currNumber;
}
})(), 3000);
</script>
</body>
</html>
And this way you can avoid the unnecessary server calls as well.
This code below works for time showing, but you have to create date.jsp page,
<%# page import="java.text.SimpleDateFormat" %>
<%# page import="java.util.Date" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.11.2.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function () {
$('#load_me').load('date.jsp').fadeIn("slow");
}, 3000);
</script>
</head>
<body>
<div id="load_me">
<%# include file="date.jsp"%>
</div>
</body>
</html>
date.jsp :
<%# page import="java.util.Date" %>
<%# page import="java.text.SimpleDateFormat" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
</head>
<body>
<%
Date d = new Date();
SimpleDateFormat sp = new SimpleDateFormat("hh:mm:ss");
String t= sp.format(d);
out.print(t);
%>
</body>
</html>

Saving selected option from combo box to file using JSP

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.

spring doesn't execute javascript

I use Spring MVC 3 in my project.
This is my AddressController :
#Controller
public class AddressController {
private static Logger logger = Logger.getLogger(AddressController.class);
#RequestMapping(value="/address",method=RequestMethod.GET)
public ModelAndView init(
#RequestParam(value="language",required=false,defaultValue="fr") String language){
Locale locale = new Locale(language);
logger.info("here");
String[] isoCountries = locale.getISOCountries();
Map<String,String> treeMap = new TreeMap<String,String>();
for(String isoCountry : isoCountries){
Locale countryLoc = new Locale(language, isoCountry);
String name = countryLoc.getDisplayCountry(locale);
if(!"".equals(name)){
treeMap.put(name,name);
}
}
Map<String,String> tree = new TreeMap<String,String>(treeMap);
ModelAndView modelAndView = new ModelAndView("address");
modelAndView.addObject("address",new Address());
modelAndView.addObject("countriesList", tree);
return modelAndView;
}
}
The first time, when I executed /address, it's going well to my controller and it returns my address.jsp by executing the javascript in this last one. But when I execute /address?language=fr or /address?language=en, the javascript code of my address.jsp is not executed.
This is a part of my address.jsp :
<%#page import="org.springframework.context.i18n.LocaleContextHolder"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%# taglib prefix="forms" uri="http://www.springframework.org/tags/form" %>
<%#page import="com.application.myGoogleAppEngine.Internationale"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# page import="java.util.Locale" %>
<%# page import="java.util.List" %>
<%# page import="java.util.ArrayList" %>
<%# page import="java.util.Collections" %>
<html>
<head>
<jsp:include page="ressources.jsp"></jsp:include>
<link rel="stylesheet" type="text/css" href="stylesheets/320x480/portrait/address.css" />
<%! Internationale internationale = Internationale.getInstance(); %>
<script>
$(document).ready(function(){
//checkParams();
alert("here");
var unit = "em";
alert("here2");
$("#backButton").attr("href","/index");
$('#validationBtn').click(function(){
var streetName = $('#streetName').val();
var streetNumber = $('#streetNumber').val();
var zipCode = $('#zipCode').val();
var city = $('#city').val();
var country = $('#country').val();
var ref = "MyServlet?streetName="+streetName+"&streetNumber="+streetNumber+"&zipCode="+zipCode+"&city="+city+"&country="+country;
$(this).attr("href",ref);
});
});
//rest of the script
</script>
<body>
<a id="backButton" data-role="button" data-icon="arrow-l"
data-ajax="false">
<spring:message code="backButton"/>
</a></div>
//rest of the code
</body>
In web MVC spring serves as server side technology. Javascript is client (browser) side technology, thus Spring "cannot" execute javascript.

Categories