2

I am setting request attribute in servlet as:

String subject = "Test Subject"; 
request.setAttribute("subject", subject);

And in jsp using JSTL setting this to

<input type='text' value='${subject}' id='subject'>

In jsp View Source it show proper value.

But in JavaScript function When i retrive value of subject by Id like

var subject = document.getElementById('subject').value;
alert(subject);

This shows some extra text gets added in subject like

Test Subject�������������������������������������������� 

My JSP Page

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
<meta charset="UTF-8">
    <title>test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/css/bootstrap.css" media="screen">
    <link rel="stylesheet" href="/css/bootstrapBlue.css" media="screen">
    <script type="text/javascript" src="/js/jquery-1.10.2.min.js"></script>
</head>
<body>
    <div class="row">
      <div class="col-lg-10">
        <h2>${subject}</h2>
        <div class="form-group">
            <input class="form-control" name="subject" id="subject" value="${subject}">
        </div>
      </div>
    </div>
    <script type="text/javascript" src="/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/js/bootstrap-switch.js"></script>
</body>
</html>
yogesh
  • 463
  • 6
  • 15

4 Answers4

3

In my case this below code works. By applying the charset to the string you will get the desired output.

request.setAttribute("subject", new String("Test Subject".getBytes(), "UTF-8"));
Dakkaron
  • 5,930
  • 2
  • 36
  • 51
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
2
request.setAttribute("subjet", subject);

There is typo issue . It should be

request.setAttribute("subject", subject);
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
  • I think that's not the problem here, the subject value is correctly inserted in the input and as the OP says the JSP output is correct so if it was about the `setAttribute` there will never be any result. – cнŝdk Dec 04 '14 at 13:58
  • add `<%@ page contentType="text/html;charset=UTF-8" language="java"%>` to the top of your jsp file. If it's not work either please post your jsp file. – Uğur Güneri Dec 04 '14 at 14:20
  • @UğurGüneri jsp added in Question. – yogesh Dec 05 '14 at 06:21
2

You can access request attribute directly with ${attrName}

your javascript code should be as below :

var subject = '${subject}';

alert(subject);

Typo is there for 'subjet'

Swap L
  • 698
  • 3
  • 12
  • 27
2

I think your problem is an Encoding problem, because your subject value is correctly inserted in the html input tag but getting this input value shows those letters, so make sure that your html page is using UTF-8 encoding, Use the following:

<head>
<meta charset="UTF-8">
</head>

Take a look at HTML Unicode (UTF-8) Reference for more information.

EDIT: Then make sure that your jsp is encoded in UTF-8 too :

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

Because JSP may have a problem with html form encoding because the HTML meta tag is ignored when the page is served over HTTP., take a look at the answer here.

Community
  • 1
  • 1
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • Ok then make sure that your project encoding is UTF-8 and not ISO8859-1 or anything else and make your jsp encoded in UTF-8 too? – cнŝdk Dec 04 '14 at 14:40