500错误
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
JSONObject jsonObject=null;
if("15912345678".equals(username)&&"123456".equals(password)){
System.out.println("username:"+username);
System.out.println("password:"+password);
jsonObject = new JSONObject("{flag:true}");
System.out.println(jsonObject);
}else {
jsonObject = new JSONObject("{flag:false}");
System.out.println(jsonObject);
}
response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
}
前台代码:
$("#login").bind("click",function () {
$.ajax({
url:"<%=basePath%>/LoginServlet",
type:"post",
data:{
username:$('input[name="username"]').val(),
password:$('input[name="password"]').val()
},
dataType:"json",
success:function (resp) {
console.log(resp);
var flag=resp.flag;
if(flag){
window.location.href="/success.jsp";
}else{
window.location.href="/fail.jsp";
}
}
})
});
浏览器显示500错误
http://localhost:8080//LoginServlet 500 (Internal Server Error)
正在回答 回答被采纳积分+1
jsonObject = new JSONObject("{flag:true}");
估计就是这里出错了,执行不下去 也不报错
你好!代码我试了一下,其实问题不大,我没有出现你的500错误,把能运行的代码给你运行一下试试吧。
success.jsp和fail.jsp放到了WebContent目录下,和login.jsp一个目录,这两个jsp文件里面只有登录成功和失败的提示内容,所以没贴代码。
如果你的还有问题,可以贴一下控制台的报错,还有浏览器报错的详细内容。祝学习愉快!
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
<style type="text/css">
input{
width:250px;
height:25px;
}
#login{
width:255px;
height:35px;
background-color:#FF2611;
border:0px;
cursor:pointer;
color:white
}
.c1{
font-size:24px;
color:black;
font-weight:bolder
}
.c2{
font-size:14px;
color:#666;
}
.c3{
font-size:14px;
color:red;
}
</style>
<script type="text/javascript" src="resources/js/jquery-1.4.2.js"></script>
</head>
<body style="text-align:center;">
<%-- <form action="<%=basePath%>/LoginServlet1" method="post"> --%>
<table>
<tr>
<td>
<span class="c1">欢迎登录</span>
<span class="c2">没有帐号?</span>
<span class="c3">立即注册</span>
</td>
</tr>
<tr>
<td><input type="text" name="username" placeholder="请输入登录邮箱/手机号"><span class="tip" style="color:red;font-size:12px"></span></td>
</tr>
<tr>
<td><input type="password" name="password" placeholder="6-16位密码,区分大小写,不能空格"></td>
</tr>
<tr>
<td>
<!-- <input type="submit" value="登录" id="login"> -->
<input type="button" value="登录" id="login">
</td>
</tr>
</table>
<!-- </form> -->
</body>
<script type="text/javascript">
$("#login").bind("click",function () {
$.ajax({
url:"<%=basePath%>/LoginServlet1",
type:"post",
data:{
username:$('input[name="username"]').val(),
password:$('input[name="password"]').val()
},
dataType:"json",
success:function (resp) {
console.log(resp);
var flag=resp.flag;
if(flag){
window.location.href="<%=basePath%>/success.jsp";
}else{
window.location.href="<%=basePath%>/fail.jsp";
}
}
})
});
</script>
</html>
LoginServlet1
package cn.java.servlet;
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 LoginServlet1
*/
@WebServlet("/LoginServlet1")
public class LoginServlet1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
JSONObject jsonObject=null;
if("imooc".equals(username)&&"123456".equals(password)){
System.out.println("username:"+username);
System.out.println("password:"+password);
jsonObject = new JSONObject("{flag:true}");
System.out.println(jsonObject);
}else {
jsonObject = new JSONObject("{flag:false}");
System.out.println(jsonObject);
}
response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- 参与学习 716 人
- 提交作业 185 份
- 解答问题 1363 个
会Java?懂前端基础?想学后台开发?那么,赶快来学习《Java Web入门》路径吧。本路径主要介绍Java Web的基础知识,并配有大量案例,定会让你收获多多!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星