点击没反应,不知道哪里出错了
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/"; %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <script type="text/javascript" src="resources/js/jquery-1.4.2.js"></script> <body> <%-- <form action="<%=basePath%>LoginServlet" method="post"> --%> <span id="tip"></span> <table> <tr><td>用户名:<input type="text" name="username"></td></tr> <tr><td>密码:<input type="password" name="password"></td></tr> <tr><td><input type="button" value="登录" id="login"></td></tr> </table> <!-- </form> --> </body> <script type="text/javascript"> $("#login").click(function(){ //单击登录按钮的时候触发ajax事件 $.ajax({ url:"<%=basePath%>/LoginServlet", type:"post", data:{ username:$("input[name=username]").val(), password:$("input[name=password]").val() }, dataType:"json", success:function(result){ var flag = result.flag; if(flag==true){ //如果登录成功则跳转到成功页面 window.location.href="<%=basePath%>success.jsp"; }else{ //跳回到Index.jsp登录页面,同时在登录页面给用户一个友好的提示 $(".tip").text("您输入的用户名或者密码不正确"); } } }); }); </script> </html> package com.imooc.ajax; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONObject; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet 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 { JSONObject jsonobject=null; String name=request.getParameter("username"); String password=request.getParameter("password"); if("123456".equals(name)&&"123".equals(password)) { System.out.println(name); System.out.println(password); jsonobject=new JSONObject("{flag:true}"); }else { jsonobject=new JSONObject("{flag:false}"); } response.getOutputStream().write(jsonobject.toString().getBytes("utf-8")); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
0
收起
正在回答
3回答
1、建议同学同外置浏览器访问一下这个index.jsp页面。
2、在浏览器端按f12快捷键,看看浏览器控制台是否有报错信息,可以贴一下报错截图:
祝学习愉快。
慕粉2034367226
2018-12-10 15:57:39
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星