当前位置: 代码迷 >> 综合 >> Servlet3.0学习记录
  详细解决方案

Servlet3.0学习记录

热度:79   发布时间:2023-12-21 22:49:57.0

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> hot3.png

servlet3.0新特性:

在servlet中配置servlet路径:     @WebServlet("/action.do")   ---->http://localhost:8080/xxx/

 

在web.xml中设置error定向:

                          <error-page>
                                  <error-code>404</error-code>
                                  <location>/error.jsp</location>
                           </error-page>

 

设置servlet:

                          <servlet>
                                  <servlet-name>SearchIp</servlet-name>
                                  <servlet-class>org.arc.servlet.SearchIp</servlet-class>
                          </servlet>
                          <servlet-mapping>
                                      <servlet-name>SearchIp</servlet-name>
                                      <url-pattern>/searchip.do</url-pattern>
                          </servlet-mapping>

 

利用servlet实现登录跳转与信息保存:

首先我们在servlet类中设置一个HttpSession对象(在doPost(...)方法中)

HttpSession session = request.getSession();

在访问数据库之后判断是否登录成功完成之后:

            1.如果成功:session = request.getSession();
                                   session.setAttribute("userid", userid);
                                    response.sendRedirect("index.jsp");

                                    设置一个seesion保存账号  并在jsp中把账号信息保存显示在页面(jsp中直接使用session对象)

            2.如果失败:

                                            PrintWriter out = response.getWriter();
                                            out.print("fuck!错误的!");

                                               暂时返回一个错误到页面