Cannot get the parameters in the server - java

I got a problem when I try to get parameters from the request, I got nothing but NULL.
The JSP file is like this:
<%# page contentType="text/html; charset=UTF-8" %>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%# taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%# taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html:html locale="true">
<head>
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="cache-control" CONTENT="no-cache">
<META HTTP-EQUIV="expires" CONTENT="0">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8">
<META HTTP-EQUIV="Content-Script-Type" content="text/javascript">
<META HTTP-EQUIV="Content-Style-Type" content="text/css">
<link rel="stylesheet" type="text/css" href="css/base.css">
<title>Menu</title>
<SCRIPT language="JavaScript">
<!--
function nextPage(url) {
location.replace(url);
}
function setUrl(url) {
document.forms[0].url.value = url;
}
//-->
</SCRIPT>
</head>
<body tabindex="-1">
<%-- ページ・ボディ --%>
<html:form action="/select.do?apl500_p=1" method="post" target="_self" >
<html:hidden property="url" value ="" />
<table style="width:100%;" border="0">
<tr>
<td style="width:100%;text-align:center">
<font size="5" color="#000000">
<B>Menu</B></font>
</td>
<td style="text-align:right">
<html:submit property="submit_logout" value="Logout" tabindex="-1"/>
</td>
</tr>
</table>
<hr><br>
<div align="center" style="width:90%;height:482px;overflow:auto;margin-left:40px;">
<table border="0">
<tr>
<td style="width:100%;text-align:center">
<html:submit property="btn1" value="アプリケーション1" onclick="setUrl('/aplXXX')" style="width:400px;height=150px;font-size:14pt;"/>
</td>
</tr>
<tr>
<td style="width:100%;text-align:center">
<html:submit property="btn2" value="新共通認証システム(ローカル環境用)テスト" onclick="setUrl('/authTest')" style="width:400px;height=150px;font-size:14pt;"/>
</td>
</tr>
</table>
</div>
<%-- ページ・フッダー --%>
<%# include file="/WEB-INF/jsp/footer.inc" %>
</html:form>
</body>
</html:html>
<% out.flush(); %>
When I used "request.getParameterNames()", I can only get the "apl500_p" parameter, I cannot get the "url" parameter. I used the tomcat7.0.41, but if I use the tomcat5.5.36,it's Ok.
I don't know why. Is there something wrong with tomcat7.0.41?
I used Fiddler2 and I'm sure that the "url" parameter was sent to the server.

Based on the discussion that you had, I suspect that the parameter url is NULL because the character encoding used by the browser is different from the character encoding used by the server.
Before the browser sends a parameter to the server, it URL-encodes the parameter. This encoding method encodes certain characters (e.g, Japanese characters) using percent encoding. Percent encoding method works by first converting the character into bytes, based on the specified character set. After that, it encodes these bytes into a sequence of hexadecimal digits, prefixed by "%" character. This character set is what I mean by character encoding used by the browser.
The server then decodes the retrieved parameter back into its original form. It does this by first decoding the sequence of hexadecimal digits into a sequence bytes. After that, it convert each bytes into a character, based on the specified character set. The problem here is that the server needs to use the same character set as the one used by the browser. It is often that the request does not carry information about the character set used by the browser, causing the server to make a best effort guess. If the guess is incorrect, the decoding process fails silently, and NULL is returned, which is what happened in your case.
Different web servers have different ways to make the guess. Web server may look at the Content-Type header, Content-Language header, Accept header, Accept Language header, configuration file, and so on.
As I am not familiar with both Tomcat versions, I am not able to pin point exactly how to fix the problem. But, you can start by studying how Tomcat figure out the character encoding. In WebSphere Application Server, there is a configuration file that can be modified to specify the default character encoding used by the server.
The parameter apl500_p is not NULL because the URL-encoded-form of 1 is also 1. The server does not have any problem decoding it back.

Try to remove the apl500_p=1 in the form action value
action="/select.do"
This way it will pass all your form elements to the servlet. Adding parameters to the form action prevents this. If you need your apl500_p value, I suggest you make another hidden field like:
<html:hidden property="apl500_p" value ="1" />

Related

How to solve HTTP Status 404

I'm trying to write a simple web app in JSP that allows user to choose 2 numbers in the range 1-100 from 2 drop-down lists and then print out those numbers. However, I keep receiving the error message:
Below is my code:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Game Table</title>
</head>
<body>
<form method="post">
#rows:
<select name="row">
<%
for(int i=1;i<=100;i++){
out.println("<option value="+"\""+i+"\""+">"+i+"</option>");
}
%>
</select>
#columns:
<select name="column">
<%
for(int i=1;i<=100;i++){
out.println("<option value="+"\""+i+"\""+">"+i+"</option>");
}
%>
</form>
<%
String row=request.getParameter("row");
String column=request.getParameter("column");
if(row!=null && column!=null){
out.println(row+" "+column);
}
%>
</body>
Thank you so much
well, problem is in navigation not in your code. For not showing dropdown menu so use proper url mapping to run your jsp like http://localhost:8080/yourwebprojectname/game.jsp game.jsp is assumption of your following above code page name.
Your code have many error like missing of action="" like
<form method="post" action="book.jsp">
</Select> tag above the form
Main problem is you dont have any button or use of ajax to show the output that you trying to display via Scriptlet
for simple solution add a button inside your form
<input type="submit" value="show" name="show"/>
add code something like this inside your Scriptlet
if("show".equals(request.getParameter("show"))){
if(row!=null && column!=null){
out.println(row+" "+column);
}
}
I am not completely sure, but it might be returning 404 (not found) because you are not submitting this data correctly. See, there is no "action" atribute indicating where it should go (e.g. "save.jsp").
Hope it helps! :)

Spring MVC having trouble getting JSP to display Controller Value

I am new to Spring MVC I have setup a test DB to try to get a single flow through and have a table displayed in a JSP page. I assume I have something configured wrong but I cannot find it. Maybe a naming convention.
I have a mySql DB
A DAO Class that just does a select * from my table that works as I can step into it.
My controller just gets a list of my Run Models. No errors are thrown The JSP simple loads an empty table. The controller seems to have the correct info in it.
Model (snippet all the getters and setters seem to work as the controller gets a list of them fine)
public class QAModel {
private int idRun;
private String SuiteID;
private String run_name;
Controller Code
#RequestMapping(value="/RunList")
public ModelAndView listRun(ModelAndView model) throws IOException{
//#ModelAttribute
System.out.println("**** Controller ******");
List<QAModel> listRun = runDao.list();
model.addObject("RunList", listRun);
model.setViewName("RunList");
return model;
}
That does get a list of Run Model Objects that contain the correct DB info I can see them if I step into it. However the JSP loads a blank table.
JSP Code (I assume I'm missing some mapping or naming convention?)
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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">
<title>Contact Manager Home</title>
</head>
<body>
<div align="center">
<h1>Contact List</h1>
<h3>New Run</h3>
<table border="1">
<th>No</th>
<th>Suite ID</th>
<th>Run Name</th>
<c:forEach var="QAModel" items="${RunList}" varStatus="status">
<tr>
<td>${status.index + 1}</td>
<td>${QAModel.SuiteID}</td>
<td>${QAModel.run_name}</td>
<td>
Edit
Delete
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
If you are not able to print any jstl variable then one possible reason can be that your jstl core tag is not added.
Add this to begining of your JSP file.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

how to Encode and decode text in Jsp

<%#page import="java.net.URLDecoder"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#page import="java.net.URLDecoder"%>
<%#page import="java.net.URLEncoder"%>
<html>
<form action="index.jsp">
<body>
First INPUT:
<input name="firstinput" type="text" name="fname">
<br>
<input type="submit" value="Submit">
<%
String first = request.getParameter("firstinput");
String Searchtext=URLDecoder.decode(first,"UTF-8");
out.println(Searchtext);
out.println(URLEncoder.encode(Searchtext,"UTF-8"));
%>
</body>
</form>
</html>
This is My code I want to Encode and Decode text in Jsp Actully I want that when Input Text " ",' ',/ /...any special charter it should print same as it is text like if Input "hello" or hello then it should Print hello or if input 'hello' then also it should Print hello... special charter should Not display please help me i am Unable to do this ...
I think you need this:
String lWithoutSpecials = first.replaceAll("[^\\p{Alpha}]+","");
For me it works great:
String s = "\\Hello\\ \"Hello\" 'Hello'";
String lWithoutSpecials = s.replaceAll("[^\\p{Alpha}]+", "");
System.out.println(lWithoutSpecials);
Output:
HelloHelloHello
You are not using full Unicode but Latin-1, ISO-8859-1. This Latin-1 will browsers
interprete as MS Windows Latin-1, or "Cp-1252"/"Windows-1252". This charset has some special characters like comma like quotes, € (euro), etcetera.
URL encoding/decoding is done automatically. The data entry of the input may
cause numeric HTML entities to arrive at the server, like Ӓ when having a restricted charset like Latin-1. With UTF-8 for the entire Unicode characters you need to add to the <form accept-charset="UTF8"> to prevent substitution by numeric entities.
A HTML 5 form:
<%#page language="java" contentType="text/html; charset=Windows-1252"
pageEncoding="Windows-1252"
import="java.net.URLDecoder"
import="java.net.URLEncoder"
%><!DOCTYPE html>
<html>
<head>
<title>First Input</title>
<meta charset="ISO-8859-1">
</head>
<body>
<form action="index.jsp">
First INPUT:
<input name="firstinput" type="text"
value="${param.firstinput}">
<br>
<input type="submit" value="Submit">
<%
String first = request.getParameter("firstinput");
String searchtext = first;
out.println(searchtext);
%>
</form>
</body>
</html>
It lies saying its charset is the limited ISO-8859-1, but java delivers the larger charset Windows-1252.
The tag <form> must be inside the <body>. If you did that for form margins and such, use CSS styles.

Composing URL in JSP

Lets say my current URL is:
/app.jsp?filter=10&sort=name.
I have a pagination component in JSP which should contain links like:
/app.jsp?filter=10&sort=name&page=xxx.
How do I create valid URLs in JSP by adding new parameters to current URL? I dont want use Java code in JSP, nor end up with URLs like:
/app.jsp?filter=10&sort=name&?&page=xxx, or /app.jsp?&page=xxx, etc.
Ok, I found answer. First problem is that I have to preserve all current parameters in URL and change only page parameter. To do this I have to iterate over all current parameters and add those I don't want to change to URL. Then I added parameters I want to either change or add. So I ended up with solution like this:
<c:url var="nextUrl" value="">
<c:forEach items="${param}" var="entry">
<c:if test="${entry.key != 'page'}">
<c:param name="${entry.key}" value="${entry.value}" />
</c:if>
</c:forEach>
<c:param name="page" value="${some calculation}" />
</c:url>
This will create nice and clean URL independent of page parameter in request. Bonus to this approach is that URL can be just anything.
<c:url var="myURL" value="/app.jsp">
<c:param name="filter" value="10"/>
<c:param name="sort" value="name"/>
</c:url>
To show the url you can do something like this
Your URL Text
To construct a new URL based on the current URL, you first need to get the current URL from the request object. To access the request object in a JSP use pageContext implicit object defined by the JSP expression language:
${pageContext.request.requestURL}
Here is the simple example of constructing URL in a JSP page:
test.jsp
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Testing URL construction</h1>
<c:choose>
<c:when test="${pageContext.request.queryString != null}">
Go to page xxx
</c:when>
<c:otherwise>
Go to page xxx
</c:otherwise>
</c:choose>
</body>
</html>
This solution allows you to construct URLs depending on whether the current URL already contains some query string or not. So you respectively append either
?${pageContext.request.queryString}&page=xxx
or just
?page=xxx
to the current URL.
JSTL and the Expression Language were used to implement checking for a query string. And we used getRequestURL() method to obtain the current URL.

Changing index.html refuse to take place in actual running of a web application

Something very weird is happening when I try to edit my index.html file .
I'm working with jsp-s and servlets , in Java.
When I try to update one of the fields , for instance :
<!DOCTYPE html>
<html>
<head><title>Bank application</title>
<link rel="stylesheet"
href="./css/styles.css"
type="text/css"/>
</head>
<body>
<table class="title">
<tr><th>Bank application</th></tr>
</table>
<br/>
<fieldset>
<legend>Bank Account Balance</legend>
<form action="show-balance">
Customer ID (id001, idffffffffffffffffdsafds002, id003):
<input type="text" name="cusdddddddddddddddddddddddddddtomerId"/><br/>
<input type="submit" value="Sddddddddddddddddddddddddddddddddddddddhow Balance"/>
</form>
</fieldset>
<br/>
// from here, the rest is the same as the above
I get this (same as before) :
Why when changing the index.html file , no change takes place ?
I'm using :
Apache 7
Xampp
Thanks
Browsers will cache HTML files unless you explicitly tell the browser to refresh (ctrl + F5). I'm guessing you're just loading the cached version of the HTML.

Categories