剑阁峥嵘而崔嵬。 2021-07-20 09:24 采纳率: 50%
浏览 66
已采纳

问,如何不用框架实现登录。

不可以用框架 。
用JSP+servlet写一个登录页面,并且登录成功后跳转主页自动查询数据库表中的所有数据(人)。
不用加密,越简单越好,急

  • 写回答

6条回答 默认 最新

  • CSDN专家-微编程 2021-07-20 11:02
    关注

    如有帮助,请采纳一下哦,谢谢O(∩_∩)O
    没有使用任何框架,简单的JSP+servlet登录代码如下
    首先两个jsp页面代码,一个登录页面代码,一个登录成功代码
    login.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>标题</title>
    <style type="text/css">
        *{margin: 0;padding: 0;}
        form{margin: 0 auto;padding:15px; width: 300px;height:300px;text-align: center;}
        #submit{padding: 10px}
        #submit input{width: 50px;height: 24px;}
    </style>
    </head>
    <body>
        <div class="wrapper">
            <form action="<%=request.getContextPath()%>/loginDemo" method="post">
                <label>用户名:</label>
                    <input type="text" name="userName" value="${param.userName}"/><br><br>
                <label>密码:</label>
                    <input type="password" name="password"/><br>
                    
                <font color="red">
                    <%
                        if(request.getAttribute("message")!= null){
                            out.print(request.getAttribute("message"));
                        }
                    %>
                </font>
                
                <div id="submit">
                    <input type="submit" value="登录"/>
                </div>
            </form>
        
        </div>
    </body>
    </html>
    

    hello.jsp欢迎界面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>标题</title>
    </head>
    <body>
        Hello:<br>
        <font color="green" size="22">
            <%
                out.print(request.getParameter("userName")+"<br>");
            %>
        </font>
        <a href="<%=request.getContextPath()%>/loginDemo/login.jsp">重新登录</a>
    </body>
    </html>
    

    然后登录serlvet代码,我建了loginDemo包,你别忘了
    对应的LoginServlet.java

    package loginDemo;
     
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    public class LoginServlet extends HttpServlet {
        
        private static final long serialVersionUID = 1L;
     
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String userName = request.getParameter("userName");
            String password = request.getParameter("password");
            System.err.println(userName+";"+password);
            String myUser = "Dong";
            String myPwd = "5432100";
            if(userName.equals(myUser)&&password.equals(myPwd)) {
                response.sendRedirect(request.getContextPath()+"/loginDemo/hello.jsp?userName="+userName);
            }else {
                request.setAttribute("message", "账密错误,请重新登录<br>");
                request.getRequestDispatcher("/loginDemo/login.jsp").forward(request, response);
            }
            
        }
     
    }
    

    对应的web.xml的配置(注册)与映射

    
      <servlet>
          <servlet-name>logindemo</servlet-name>
          <servlet-class>loginDemo.LoginServlet</servlet-class>
      </servlet>
      <servlet-mapping>
          <servlet-name>logindemo</servlet-name>
          <url-pattern>/loginDemo</url-pattern>
      </servlet-mapping>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 已采纳回答 7月20日
  • 创建了问题 7月20日

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退