I keep getting a Null Pointer Exception, and I can't tell from the stack trace exactly what is causing it. It seems to point to line 83 in DisplayAccounts.jsp but the file is only 48 lines long...
After logging in, the LoginServlet is supposed to direct user to DisplayAccounts.jsp
Any help would be greatly appreciated. I can't seem to figure it out.
DisplayAccounts.jsp:
<h1>Customer Accounts</h1>
<table>
<tr>
<th>Account Number</th>
<th>Customer ID</th>
<th>Account Type</th>
<th>Account Balance</th>
</tr>
<% ArrayList accounts = (ArrayList)session.getAttribute("customer.list");
for(int i = 0; i < accounts.size(); i++) {
Account account = (Account)accounts.get(i); %>
<tr>
<td><% out.print(account.getAcctNo());%></td>
<td><% out.print(account.getcID());%></td>
<td><% out.print(account.getType());%></td>
<td><% out.print(account.getBalance());%></td>
</tr>
<% } %>
</table>
LoginServlet.java :
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String id;
String pw;
int idDB;
String pwDB;
HttpSession session;
try{
id = request.getParameter("custID");
pw = request.getParameter("pswd");
System.out.println("ID: " + id);
System.out.println("Password: " + pw);
idDB = Integer.parseInt(id);
Customer c1 = new Customer();
c1.selectDB(idDB);
pwDB = c1.getCustPassword();
// Add user to session
session = request.getSession();
session.setAttribute("customer", c1);
System.out.println("User added to session");
if (pw.equals(pwDB)){
// Send user to account look up page
RequestDispatcher rd = request.getRequestDispatcher("DisplayAccount.jsp");
rd.forward(request, response);
}
else{
// Send user to error page
RequestDispatcher rd = request.getRequestDispatcher("ErrorPage.jsp");
rd.forward(request, response);
}
}
catch(ServletException e){
System.out.println("A Servlet Excpetion has occured");
System.out.println(e.getMessage());
System.out.println(e.getStackTrace());
}
catch(IOException e){
System.out.println("An IO Exception has occured");
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println("An error has occurred.");
System.out.println(e.getMessage());
}
}
Exception Stack Trace:
Info: A Servlet Excpetion has occured
Info: java.lang.NullPointerException
Info: java.lang.NullPointerException
at org.apache.jsp.DisplayAccount_jsp._jspService(DisplayAccount_jsp.java:83)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:575)
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:546)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
at Chatt_Bank.LoginServlet.processRequest(LoginServlet.java:45)
at Chatt_Bank.LoginServlet.doPost(LoginServlet.java:80)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:748)
Here
ArrayList accounts = (ArrayList)session.getAttribute("customer.list");
accounts is null. You haven't set a HttpSession parameter with a name of customer.list using
HttpSession#setAttribute(String name, Object o)
(setAttribute) inside your LoginServlet#processRequest method. So, when
if (pw.equals(pwDB)){
You're forwarding to DisplayAccount.jsp without that value.
You need to add
session.setAttribute("customer.list", customerList);
You cannot do what you want to do, you need another attribute
session.setAttribute("customerList", c1.list); // or c1.getList() if you have a getter
Related
This is my code for a web application that connects to a mySQL database. It is supposed to display the entire contents of one table in the database using a drop down menu on another page. I have another .jsp that does basically the same thing with a different SQL statement that performs a search operation, that works as it is supposed to. I figured that if anything I would have a problem with procedural generation of the output html tables, but I am getting a com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException exception for the mySQL statement syntax.
<%--
Document : display
Created on : Nov 22, 2018, 11:43:54 AM
Author : Scott
--%>
<%#page import="java.sql.*" %>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
System.out.println("Unable to load driver class!");
}
%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Henry Books</title>
</head>
<body>
<h1>Henry Books Store Database Table View </h1>
<%!
public class Table {
String URL = "jdbc:mysql://localhost:3306/HenryBooks?autoReconnect=true&useSSL=false";
String USERNAME = "root";
String PASSWORD = "SAvick4078100";
Connection connection = null;
PreparedStatement viewTable = null;
ResultSet resultSet = null;
public Table(){
try{
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
viewTable = connection.prepareStatement(
"SELECT * FROM ?"
);
} catch (SQLException e) {
e.printStackTrace();
}
}
public ResultSet getTable(String tableSelection) {
try{
viewTable.setString(1, tableSelection);
resultSet = viewTable.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return resultSet;
}
}
%>
<%
String tableSelect = new String();
//recieves input value from dropdown menu name "table option" in index.jsp
if(request.getParameter("table option") != null) {
tableSelect = request.getParameter("table option");
}
Table view = new Table();
ResultSet tableView = view.getTable(tableSelect);
ResultSetMetaData rsMetaData = tableView.getMetaData();
int cols = rsMetaData.getColumnCount();
String[] colNames = new String[cols];
for(int i = 0; i < cols; i++) {
colNames[i] = rsMetaData.getColumnName(i+1); //i+1 because the column name index in the ResultSetMetaData begins at 1, not 0
}
%>
<table border="1">
<thead>
<tr>
<% for(int i = 0; i < cols; i++) { %>
<th><%= tableView.getString(colNames[i])%></th>
<% } %>
</tr>
</thead>
<tbody>
<% while(tableView.next()) { %>
<tr>
<% for(int i = 0; i < cols; i++) { %>
<td><%= tableView.getString(colNames[i])%></td>
<% } %>
</tr>
<% } %>
</tbody>
</table>
</body>
</html>
However, I am receiving these error messages and I am not sure what is wrong with my SQL syntax here.
Severe: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Author'' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3978)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3914)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2495)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1903)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2011)
at org.apache.jsp.display_jsp$Table.getTable(display_jsp.java:36)
at org.apache.jsp.display_jsp._jspService(display_jsp.java:113)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:748)
Warning: StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.display_jsp._jspService(display_jsp.java:114)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:748)
using
"SELECT * FROM ?"
for dynamically adding table name will lede to query being
SELECT * FROM 'TABLE_NAME'
instead append table name dynamically by just contacting the string like this:
"SELECT * FROM "+tableName
where tableName will be a variable of type string.
I'm pretty new to programming and i encountered this problem i have search internet and couldn't find the answer i was looking for. Hope you can help me here. Thanks!
I'm trying to pass 2 variables which is the name and custid.
(i'm using mysql workbench as my database).
I'm guessing my error is something to do with.
String email=request.getParameter("email");
String password=request.getParameter("password");
here's my code:
#WebServlet("/login")
public class loginServlet extends HttpServlet{
#Resource(name="jdbc/eg3752")
private DataSource usercatalogue;
#Override
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
String email=request.getParameter("email");
String password=request.getParameter("password");
Connection connection=null;
Statement statement=null;
ResultSet resultset=null;
String login="SELECT * FROM customer where email='"+email+"'";
try
{
connection=usercatalogue.getConnection();
statement=connection.createStatement();
resultset=statement.executeQuery(login);
while(resultset.next())
{
// check if password is same in the database
if(password.equals(resultset.getString("password")))
{
HttpSession session =request.getSession();
session.setAttribute("name", resultset.getString("fullname"));
session.setAttribute("custid", resultset.getString("customerId"));
response.sendRedirect(this.getServletContext().getContextPath()+"/search.html");
}
else
{
response.sendRedirect(this.getServletContext().getContextPath() + "/login.html");
}
}
}
catch(Exception e)
{
e.printStackTrace();
System.err.println(e.getMessage());
}
}
}
and this is the error on the GlassFish Server 4
Severe: java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:518)
at com.aleksi.loginServlet.doPost(loginServlet.java:58)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)
You did not include the "caused by" sections of the stack trace, which are the important part.
An IllegalStateException on a sendRedirect() has to do with the HTTP protocol and occurs when the response to the client has already been committed. This will usually happen when either the response buffer has reached max size or someone has called flush() explicitly or written something to the output stream and caused the response to be committed.
After calling sendRedirect() add return; and then try.
I'm getting this exception in my Servlet class, when I'm trying to upload file from client app to my server, this is my servlet doPost code fragment
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String userName = "admin";
String password = "123";
if (DBManager.getInstance().checkIfCanLogin(userName, password)) {
final Part part = (Part) request.getParts().toArray()[1];
new Thread(() -> {
// bla bla bla
}).start();
} else {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}
}catch (Exception e){
e.printStackTrace();
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
here the line final Part part = (Part) request.getParts().toArray()[1]; throws exception, I'm trying to fix this problem hole day but nothing found, also I'm new in JavaEE, my background is from android, also what I'm trying to understan is that everything worked properly, but after restarting (I think nothing more have done with it) this Servlet stoped working, problem has only this servlet, where I'm sending files from client (andorid using retrofit)
this is error log
[2017-05-02T18:45:45.472+0400] [glassfish 4.1] [SEVERE] [] [] [tid: _ThreadID=29 _ThreadName=Thread-9] [timeMillis: 1493736345472] [levelValue: 1000] [[
java.io.EOFException
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.read(TCPNIOTransport.java:607)
at org.glassfish.grizzly.nio.transport.TCPNIOTemporarySelectorReader.readNow0(TCPNIOTemporarySelectorReader.java:65)
at org.glassfish.grizzly.nio.tmpselectors.TemporarySelectorReader.read0(TemporarySelectorReader.java:171)
at org.glassfish.grizzly.nio.tmpselectors.TemporarySelectorReader.read0(TemporarySelectorReader.java:141)
at org.glassfish.grizzly.nio.tmpselectors.TemporarySelectorReader.read(TemporarySelectorReader.java:113)
at org.glassfish.grizzly.nio.tmpselectors.TemporarySelectorReader.read(TemporarySelectorReader.java:75)
at org.glassfish.grizzly.AbstractReader.read(AbstractReader.java:72)
at org.glassfish.grizzly.nio.transport.TCPNIOTransportFilter.handleRead(TCPNIOTransportFilter.java:77)
at org.glassfish.grizzly.filterchain.TransportFilter.handleRead(TransportFilter.java:173)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.read(DefaultFilterChain.java:351)
at org.glassfish.grizzly.filterchain.FilterChainContext.read(FilterChainContext.java:736)
at org.glassfish.grizzly.http.io.InputBuffer.blockingRead(InputBuffer.java:1107)
at org.glassfish.grizzly.http.server.io.ServerInputBuffer.blockingRead(ServerInputBuffer.java:95)
at org.glassfish.grizzly.http.io.InputBuffer.fill(InputBuffer.java:1131)
at org.glassfish.grizzly.http.io.InputBuffer.read(InputBuffer.java:348)
at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:267)
at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:270)
at org.apache.catalina.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:977)
at org.apache.catalina.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:888)
at java.io.InputStream.read(InputStream.java:101)
at org.apache.catalina.fileupload.Streams.copy(Streams.java:137)
at org.apache.catalina.fileupload.Streams.copy(Streams.java:107)
at org.apache.catalina.fileupload.Multipart.initParts(Multipart.java:154)
at org.apache.catalina.fileupload.Multipart.getParts(Multipart.java:175)
at org.apache.catalina.connector.Request.getParts(Request.java:4507)
at org.apache.catalina.connector.RequestFacade.getParts(RequestFacade.java:1086)
at servlet.UploadServlet.doPost(UploadServlet.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:748)]]
I'm trying to add items using a JSP page which has a couple of images too. I declared the variables to retrieve images as strings so i can get the URL and store the url in the database.
I'm using EJB and JPA for database purposes.
my code for the servlet
ItemDetails id;
ItemBeanRemote ib;
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String name = request.getParameter("name");
double price = Double.valueOf(request.getParameter("price"));
String imgurl = request.getParameter("imgurl");
String imgurl2 = request.getParameter("imgurl2");
String location = request.getParameter("loc");
id = new ItemDetails(id, name, price, imgurl, imgurl2, location);
ib.addItem(id);
response.sendRedirect("View.jsp");
}
#Override
public void init() {
try {
Context initial = new InitialContext();
ib = (ItemBeanRemote) initial.lookup("itembean");
} catch (Exception ex) {
System.err.println("Caught an exception:");
ex.printStackTrace();
}
//ejb class that has the code to the addItem method
#Override
public void addEvent(ItemDetails details) {
try {
tblItem utt = new tblItem(details.getItemID(),details.getItemName(),
details.getPrice(),details.getImgUrl(),details.getImgUrl2(),details.getLocation());
em.persist(utt);
} catch (Exception ex) {
throw new EJBException(ex);
}
//item details is a class is a POJO with getters and setters
//tblItem is the java class that was created using an entity manager which has auto generated code from JPA.
And the exception
javax.ejb.EJBException: java.rmi.MarshalException: CORBA MARSHAL 1330446343 No; nested exception is:
org.omg.CORBA.MARSHAL: ----------BEGIN server-side stack trace----------
org.omg.CORBA.MARSHAL: FINE: 00810007: Underflow in BufferManagerReadStream after last fragment in message vmcid: OMG minor code: 7 completed: No
at com.sun.proxy.$Proxy154.endOfStream(Unknown Source)
at com.sun.corba.ee.impl.encoding.BufferManagerReadStream.underflow(BufferManagerReadStream.java:122)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_1.grow(CDRInputStream_1_1.java:111)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_2.alignAndCheck(CDRInputStream_1_2.java:126)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_0.read_long(CDRInputStream_1_0.java:433)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_0.readValueTag(CDRInputStream_1_0.java:1672)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_0.read_value(CDRInputStream_1_0.java:918)
at com.sun.corba.ee.impl.encoding.CDRInputObject.read_value(CDRInputObject.java:518)
at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl$14.read(DynamicMethodMarshallerImpl.java:383)
at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl.readArguments(DynamicMethodMarshallerImpl.java:450)
at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:171)
at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528)
at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549)
at com.sun.corba.ee.impl.protocol.SharedCDRClientRequestDispatcherImpl.marshalingComplete(SharedCDRClientRequestDispatcherImpl.java:119)
at com.sun.corba.ee.impl.protocol.ClientDelegateImpl.invoke(ClientDelegateImpl.java:258)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:198)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:150)
at com.sun.corba.ee.impl.presentation.rmi.codegen.CodegenStubBase.invoke(CodegenStubBase.java:226)
at com.olympics2016.beans.__EventBeanRemote_Remote_DynamicStub.addEvent(com/olympics2016/beans/__EventBeanRemote_Remote_DynamicStub.java)
at com.olympics2016.beans._EventBeanRemote_Wrapper.addEvent(com/olympics2016/beans/_EventBeanRemote_Wrapper.java)
at com.olympics2016.servlets.AddEventServlet.doPost(AddEventServlet.java:42)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
----------END server-side stack trace---------- vmcid: OMG minor code: 7 completed: No
Marshal Exception can occur due to one of the reasons as below :
1) when remote method call is done and no marshaling is done of Java Object and thus the argument or return value will not reach to remote method or to server.
2) If the protocol we are using in RMI is of different versions at sender and receivers side.
I wanted to list all countries to fetch all the respective state and currency.
ConfFacade.java
public Result<List<Country>> listCountries() {
try {
String HQL = "SELECT DISTINCT o FROM Ocountry o LEFT JOIN FETCH o.ostate, o.ocurrency";
List<Country> list = em.createQuery(HQL).getResultList();
return new Result(Status.SUCCESS, list);
} catch(Exception e) {
e.printStackTrace();
return new Result<>(Status.ERROR, Exception.GENERAL, e.getMessage());
}
}
ConfFacadeLocal.java
public interface ConfFacadeLocal {
public Result<List<Country>> listCountries();
}
This is the jsp that I use and code it:
listCountries.jsp
<%
try {
InitialContext ctx = new InitialContext();
ConfFacadeLocal confFacadeLocal = (ConfFacadeLocal)ctx.lookup("java:comp/env/tomin");
Result<List<Country>> result = confFacadeLocal.listCountries();
if(result.isUnsuccessful()) {
throw new Exception(result.getErrMsg());
}
List<Country> countryList = result.getObject();
if(countryList == null) {
throw new Exception("Error.");
}
out.print("OK");
%>
<table border="1" align="center" style="width:50%">
<thead>
<tr>
<th>country code</th>
<th>currency code</th>
<th>state id</th>
<th>state description</th>
</tr>
</thead>
<tbody><%
for (Ocountry country : countryList){ %>
<tr>
<td><%=country.getOccode()%></td>
<td><%=country.getOcurrency().getOcucode()%></td>
<td><%=country.getOstate().getOsid()%></td>
<td><%=country.getOstate().getOsdesc()%></td>
</tr><%
}
} catch (Exception e) {
e.printStackTrace();
out.print("Error: " + e.getMessage());
} %>
</tbody>
</table>
Here is the error that I get:
cannot find symbol
symbol: method getOstate()
location: variable country of type com.ejb.entities.conf.Country
PWC6197: An error occurred at line: 42 in the jsp file: /country/listCountries.jsp
PWC6199: Generated servlet error:
cannot find symbol
symbol: method getOstate()
location: variable country of type com.ejb.entities.conf.Country
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:129)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:299)
at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:392)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:453)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:625)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
Can anyone help to fix it?
From your JSP error logs, I guess you missed to declare the #listCountries method to the ConfFacadeLocal bean interface.
Adding the needed method declaration to the interface should resolve the issue:
public interface ConfFacadeLocal {
// other API methods ...
public Result<List<Country>> listCountries();
}
There (line 41):
<td><%=currency.getOcucode()%></td>
you are using the currency variable, which has not been declared.
It seems that currency is an attribute of the 'Country' class in you program, so maybe you should write instead
<td><%=country.getCurrency().getOcucode()%></td>
You have the same problem with the 'state' variable.