我的Servlet代码:
@WebServlet(description = "A simple", urlPatterns = { "/SimpleServlet" })
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter wr = response.getWriter();
String uname = request.getParameter("unam");
HttpSession ses = request.getSession();
if (uname != "" && uname != "null") {
ses.setAttribute("userses", ses);
}
wr.println("In RequestHttp method your name is "+ uname);
wr.println("In Sesssion method your name is " + (String)ses.getAttribute("userses"));
}
}
`Apache Tom Cat 7:0错误严重:servlet的Servlet.service() [org.sathish.brains.SimpleServlet]与路径的上下文 [/ SimpleServletProject]抛出异常java.lang.ClassCastException: org.apache.catalina.session.StandardSessionFacade无法强制转换为 java.lang.String at org.sathish.brains.SimpleServlet.doGet(SimpleServlet.java:37)at at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)at at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)at at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044) 在 org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:607) 在 org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:313) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:744)
答案 0 :(得分:1)
看看这一行:
ses.setAttribute("userses", ses);
您将自己的会话作为属性传递。我认为您尝试做的事情就像是
ses.setAttribute("userses", uname);
答案 1 :(得分:0)
if (uname != "" && uname != "null") {
ses.setAttribute("userses", ses);
}
您真的想要将会话添加到会话中吗?我怀疑,您可能希望将属性设置为uname
。更加注意你的代码。
将转换移除到String,这是不必要的。
在您尝试编写servlet代码之前,认真了解Java的基础知识。