JSP cannot execute SQL in java class - java

I am using eclipse, wamp and tomcat to manage a project. I can run my java class and call java class with JSP. But when I use JSP call Java class, SQL in Java class not execute.
My code SQL in Java class
public P_Process() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/myproject?autoReconnect=true&useSSL=false",
"root", "");
st = con.createStatement();
} catch (Exception ex) {
System.out.println("Error: " + ex);
}
}
Output :
Table A Get Data Complete!!!!!
names : 1611
Table B Get Data Complete!!!!!
tf_idf2 : 102560
Table C Get Data Complete!!!!!
idf2 : 4804
value of segmentation ............ red|black|
input >>>>>>>> red,black
input size : 2
My code JSP call Java class
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# page import="com.sample.P_Process"%>
<!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>Insert title here</title>
</head>
<body>
<%
P_Process start = new P_Process();
out.print(start.process("red,black"));
%>
</body>
</html>
Output :
Error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
java.lang.NullPointerException
Table A Get Data Complete!!!!!
names : 0
java.lang.NullPointerException
Table B Get Data Complete!!!!!
tf_idf2 : 0
java.lang.NullPointerException
Table C Get Data Complete!!!!!
idf2 : 0
value of segmentation ............ red|black|
input >>>>>>>> red,black,
input size : 2

Related

Enumeration<> Enumeration difference

I'm a biginner in JSP and I'm confused about the difference between
Enumeration and Enumeration<type>
I'm learning with this Korean book and the example source in it says Enumeration
with the eclipse neon version it doesn't work. It works only when it write
Enumeration<String>. Can someone tell me the difference?
<%#page import="java.util.Enumeration"%>
<%# 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>헤더 목록 출력</title>
</head>
<body>
<%
Enumeration<String> headerEnum = request.getHeaderNames();
while(headerEnum.hasMoreElements()){
String headerName = (String)headerEnum.nextElement();
String headerValue = request.getHeader(headerName);
%>
<%=headerName %> = <%=headerValue %> <br>
<%
}
%>
</body>
</html>
Just give it a look at the Enumeration documentation. Also review the generic types documentation.
By usingEnumeration you are using Enumeration<Object> as it is the default. What this <Object> does is just indicate the Enumeration class that in that particular instance, the type that it calls E (in the Enumeration Documentation) will be resolved to Object. By using <String> happens the same: the type called E will be resolved to String.
If you check the nextElement() signature it returns E. So, by using Enumeration or Enumeration<Object> that method will return Object and you will need the cast you did:
String headerName = (String)headerEnum.nextElement();
By using Enumeration<String> the method will return an String, so you can directly do this:
String headerName = headerEnum.nextElement();

Unable to decode utf-8 using thai language?

This is my JSP page:
<%--
Document : new jsp1
Created on : Nov 24, 2014, 12:38:07 PM
Author : Java
--%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String msg="206_John_help i m in trouble,delhi,อินเดีย_30.64741430_76.817313799";
String result = java.net.URLEncoder.encode(msg, "UTF-8");
System.out.println("The msg is "+result);
String result1=java.net.URLDecoder.decode(result, "UTF-8");
System.out.println("The decoded msg is "+result1);
%>
</body>
</html>
The output is 206_John_help i m in trouble,delhi,???????_30.64741430_76.817313799
I am always getting ?????? instead of thai alphabets. How can I get the Thai alphabets while decoding?
The problem is not on encoding and decoding the messages but in the server container. Seems that server cant display properly the special characters, so it can not treat them from request.
If you display the values in the JSP page itself, everything is working fine.
Example:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String msg="206_John_help i m in trouble,delhi,อินเดีย_30.64741430_76.817313799";
System.out.println("The original message "+msg);
String result = java.net.URLEncoder.encode(msg, "UTF-8");
System.out.println("The msg is "+result);
String result1=java.net.URLDecoder.decode(result, "UTF-8");
System.out.println("The decoded msg is "+result1);
%>
Original message <%=msg %><br />
Encrypted message <%=result %> <br />
Decrypted message <%=result1 %>
</body>
</html>

jsp: how to get value from session?

I get an String array from session and I want to show the String from the array.
I know javascript can't get data from session directly. Is there any method I can get the data from session and transfer it to javascrip?
My code as follows:
<%# 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">
<%String[] sele = (String[])session.getAttribute("selections");%>;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Expires" content="0">
<meta http-equiv="kiben" content="no-cache">
<title>Check List</title>
<script language="javascript" type="text/JavaScript" src="cookie.js">
</script>
<script text="text/javascript">
{
var selections = //String array form session
for(var v=0;v<selections.length;v++){
fillForm(selections[v]);
}
}
function fillForm(name){
var checkbox= document.createElement("input");
checkbox.type="checkbox";
checkbox.name=name;
checkbox.value=name;
checkbox.id=name;
var label = document.createElement("label");
label.htmlFor="id";
label.appendChild(document.createTextNode(name));
var container = document.getElementById("checklist");
container.appendChild(checkbox);
container.appendChild(label);
container.appendChild(document.createElement("br"));
}
function submitAction(){
addUserName(document.getElementById("checklist"));
var elem = document.getElementById("checklist").elements;
for(i =0;i<elem.length;i++){
elem[i].checked = true;
}
var form = document.getElementById("checklist");
form.submit();
}
</script>
</head>
<body>
<form id="checklist" action="selection">
</form>
<Button type="button" onclick="submitAction()" name="submit">Submit</button>
</body>
</html>
Simply use JSP Expression Language and JSP JSTL
<script>
alert("value: ${selections}");
</script>
Here selections is an attribute that is set in any scope page, request, session or application.
You can directly access an attribute form session scope:
{sessionScope.selections}
Note: I don't know that Java ArrayList does work in JavaScript as well. If it doesn't work then simply set a comma separated string as session attribute and split it in JavaScript as shown below.
Sample code:
<script>
var selections = "${sessionScope.csv}".split(",");
for ( var v = 0; v < selections.length; v++) {
alert(selections[v]);
}
</script>
Here csv is a comma separated string value that is set in session scope.
Use JSP JSTL and EL instead of Scriplet that is more easy to use and less error prone.
You can achieve it in JSTL without using JavaScript. Simply iterate the list using <c:forEach> tag and add that much of check boxes and labels.
Sample code:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
...
<body>
...
<c:forEach items="${selections }" var="name">
<input type="checkbox" name="${name}" value="${name}" id="${name}">
...
</c:forEach>
</body>

How to use jsp variable in javascript array?

Hi I have a jsp page where I have some variable. I want to access the variable in a javascript array. How can I get this?
Demo.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
int i=1;
int j=2;
int k=3;
int l=4;
%>
</body>
</html>
I want to use these 4 variables in the javascript array and print them.
How can I achieve this?
Try,
<script language="JavaScript">
var Arr = new Array();
Arr[0] = '<%=i %>';
Arr[1] = '<%=j %>';
Arr[2] = '<%=k %>';
Arr[3] = '<%=l %>';
</script>
In Java script you have to use scriplet tag to use jsp data.
<%
Integet a = i ; //here i is your jsp variable
%>
In order to make this work you should declare the variable before using it (as always):
<%
String myVar="blabla";
%>
<script type="text/javascript">
foo();
function foo() {
var value = "<%=myVar%>";
alert(value);
}
</script>
Or :
var result = [];
result.push(<%i%>);
result.push(<%j%>);
result.push(<%k%>);
result.push(<%l%>);
You can access it using JSTL.
set java variable to JSTL variable
Access it in Java script as
var x='${jstl_varialbe_goes_here
}';
alert(x);

unable to display all info from the database through jsp

My requirement is to insert artist details and his picture through jsp into oracle database and retrieve back information and picture through another jsp program.
artist table has five columns, four are varchar2 and fifth column is blob type.
I have successfully inserted and successfully able to retrieve but the problem it displays only image. Below is the code. I am stuck. I need help. Please suggest me.
PreparedStatement ps=con.prepareStatement("select * from artist");
ResultSet rs=ps.executeQuery();
while(rs.next()){ %>
<table><tr><th>artist fast name:</th><td><%=rs.getString(1) %></td></tr>
<tr><th>artist middle name:</th><td><%=rs.getString(2) %></td></tr>
<tr><th>artist last name</th><td><%=rs.getString(3) %></td></tr>
<tr><th>artist job</th><td><%=rs.getString(4) %></td></tr>
<tr><th>artist image</th><td><img src="
<%
Blob bl=rs.getBlob(5);
byte[] image=bl.getBytes(1, (int)bl.length());
response.setContentType("image/jpeg");
OutputStream o = response.getOutputStream();
o.write(image);
o.flush();
o.close();
}
%>" height="100" width="100" alt="bye"/> </td></tr>
</table>
<%
con.close();
As of version 6, Java SE provides JAXB by which the bytes may be converted in to base64 string. Here also you may convert the image byte[] into base 64 string and it can be displayed using the <img html tag specifying the src data as base 64 i.e. src="data:image/png;base64,.
Modify your code as follows :
<%
PreparedStatement ps=con.prepareStatement("select * from artist");
ResultSet rs=ps.executeQuery();
while(rs.next()){ %>
<table><tr><th>artist fast name:</th><td><%=rs.getString(1) %></td></tr>
<tr><th>artist middle name:</th><td><%=rs.getString(2) %></td></tr>
<tr><th>artist last name</th><td><%=rs.getString(3) %></td></tr>
<tr><th>artist job</th><td><%=rs.getString(4) %></td></tr>
<tr><th>artist image</th><td>
<%
Blob bl=rs.getBlob(5);
byte[] image=bl.getBytes(1, (int)bl.length());
%>
<img src="data:image/jpeg;base64, <%=javax.xml.bind.DatatypeConverter.printBase64Binary(image)%>
" height="100" width="100" alt="bye"/> </td></tr>
</table>
<%
}
con.close();
%>
Here is another sample jsp page for getting a clear idea :
<%#page import="java.awt.image.BufferedImage"%>
<%#page import="javax.imageio.ImageIO"%>
<%#page import="java.io.*"%>
<%# 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>Insert title here</title>
</head>
<body>
<%
BufferedImage bImage = ImageIO.read(new File("/home/visruth/Desktop/Visruth.jpg"));//give the path of an image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bImage, "jpg", baos );
baos.flush();
byte[] imageInByteArray = baos.toByteArray();
baos.close();
String b64 = javax.xml.bind.DatatypeConverter.printBase64Binary(imageInByteArray);
%>
<div>
<p>As of v6, Java SE provides JAXB</p>
<img src="data:image/jpg;base64, <%=b64%>" alt="Visruth.jpg not found" />
</div>
</body>
</html>

Categories