各位大神好,我又回来了,一个很紧急的问题,可能我基础太差,想了一下午没想出来,现在真心求助。
是这样,我写了一个servlet处理数据库查询结果,再将url指向我的主页面,在主页面中获取servlet参数,同时主页面有一个ajax,可以提交内容到另一个页面进行数据库的储存。相当于一个A-B-C的过程。但是我的ajax在添加servlet代码之前是正常的,加了以后,一直报404错误,不知道为啥。servlet页面主要代码如下:protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动
String url="jdbc:mysql://localhost:3306/s.a.v.e?characterEncoding=utf8&useSSL=false";
String username="root";
String password="123456";
Connection conn=DriverManager.getConnection(url,username,password);//连接数据库
Statement state=conn.createStatement();
String sql="select * from blog ORDER BY date ASC";
ResultSet rs=state.executeQuery(sql);
List<Content> list=new ArrayList<Content>();
while(rs.next()) {
Content content=new Content();//实例化
content.setTitle(rs.getString("title"));
content.setContent(rs.getString("content"));
content.setDate(rs.getString("date"));
content.setFusername(rs.getString("fusername"));
list.add(content);
}
request.setAttribute("list", list);//将内容对象放置于集合中
}catch(Exception e) {
e.printStackTrace();
}
String username=request.getParameter("username");
request.getRequestDispatcher("/xiaochuangchangtan/windowrest.jsp?username="+username).forward(request, response);
}
这个servlet指向我的主页面并传递一个参数username。我的主页面部分代码如下:
<script type="text/javascript">
/**
* Ajax
*/
function createRequest(url) {
http_request = false;
if (window.XMLHttpRequest) { // 非IE浏览器
http_request = new XMLHttpRequest(); //创建XMLHttpRequest对象
} else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP"); //创建XMLHttpRequest对象
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP"); //创建XMLHttpRequest对象
} catch (e) {
}
}
}
if (!http_request) {
alert("不能创建XMLHttpRequest对象实例!");
return false;
}
http_request.onreadystatechange = getResult; //调用返回结果处理函数
http_request.open('GET', url, true); //创建与服务器的连接
http_request.send(null); //向服务器发送请求
} function getResult() {
if (http_request.readyState == 4) { // 判断请求状态
if (http_request.status == 200) { // 请求成功,开始处理返回结果
alert(http_request.responseText); //设置提示内容
} else { // 请求页面有错误
alert("您所请求的页面有错误!"+http_request.readyState+http_request.status);
}
}
}
/**
* 获取url参数中的username
*/
function d() {
var url = location.search; //获取url中"?"符后的字串
if (url.indexOf("?") != -1) { //判断是否有参数
var str = url.substr(1); //从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串
strs = str.split("="); //用等号进行分隔 (因为知道只有一个参数 所以直接用等号进分隔 如果有多个参数 要用&号分隔 再用等号进行分隔)
strs[1]; //直接弹出第一个参数 (如果有多个参数 还要进行循环的)
}
return strs[1];
}
/**
* 发表功能函数
*/
function addDiv(){
var username=d();
//创建div
var articlediv = document.createElement('div');
//获取值
var title=document.getElementById("title").value;
var content=document.getElementById("aticle").value;
if(title=="" || content==""){
alert("请输入标题或内容");
}else{
//发表到后台
createRequest(encodeURI(encodeURI('AddContent.jsp?title='+title+'&aticle='+content)));
}
}
</script>
主函数是addDiv(),中间有个无用的获取url中username参数的方法,那个也无关紧要了。
是这样,我写了一个servlet处理数据库查询结果,再将url指向我的主页面,在主页面中获取servlet参数,同时主页面有一个ajax,可以提交内容到另一个页面进行数据库的储存。相当于一个A-B-C的过程。但是我的ajax在添加servlet代码之前是正常的,加了以后,一直报404错误,不知道为啥。servlet页面主要代码如下:protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动
String url="jdbc:mysql://localhost:3306/s.a.v.e?characterEncoding=utf8&useSSL=false";
String username="root";
String password="123456";
Connection conn=DriverManager.getConnection(url,username,password);//连接数据库
Statement state=conn.createStatement();
String sql="select * from blog ORDER BY date ASC";
ResultSet rs=state.executeQuery(sql);
List<Content> list=new ArrayList<Content>();
while(rs.next()) {
Content content=new Content();//实例化
content.setTitle(rs.getString("title"));
content.setContent(rs.getString("content"));
content.setDate(rs.getString("date"));
content.setFusername(rs.getString("fusername"));
list.add(content);
}
request.setAttribute("list", list);//将内容对象放置于集合中
}catch(Exception e) {
e.printStackTrace();
}
String username=request.getParameter("username");
request.getRequestDispatcher("/xiaochuangchangtan/windowrest.jsp?username="+username).forward(request, response);
}
这个servlet指向我的主页面并传递一个参数username。我的主页面部分代码如下:
<script type="text/javascript">
/**
* Ajax
*/
function createRequest(url) {
http_request = false;
if (window.XMLHttpRequest) { // 非IE浏览器
http_request = new XMLHttpRequest(); //创建XMLHttpRequest对象
} else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP"); //创建XMLHttpRequest对象
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP"); //创建XMLHttpRequest对象
} catch (e) {
}
}
}
if (!http_request) {
alert("不能创建XMLHttpRequest对象实例!");
return false;
}
http_request.onreadystatechange = getResult; //调用返回结果处理函数
http_request.open('GET', url, true); //创建与服务器的连接
http_request.send(null); //向服务器发送请求
} function getResult() {
if (http_request.readyState == 4) { // 判断请求状态
if (http_request.status == 200) { // 请求成功,开始处理返回结果
alert(http_request.responseText); //设置提示内容
} else { // 请求页面有错误
alert("您所请求的页面有错误!"+http_request.readyState+http_request.status);
}
}
}
/**
* 获取url参数中的username
*/
function d() {
var url = location.search; //获取url中"?"符后的字串
if (url.indexOf("?") != -1) { //判断是否有参数
var str = url.substr(1); //从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串
strs = str.split("="); //用等号进行分隔 (因为知道只有一个参数 所以直接用等号进分隔 如果有多个参数 要用&号分隔 再用等号进行分隔)
strs[1]; //直接弹出第一个参数 (如果有多个参数 还要进行循环的)
}
return strs[1];
}
/**
* 发表功能函数
*/
function addDiv(){
var username=d();
//创建div
var articlediv = document.createElement('div');
//获取值
var title=document.getElementById("title").value;
var content=document.getElementById("aticle").value;
if(title=="" || content==""){
alert("请输入标题或内容");
}else{
//发表到后台
createRequest(encodeURI(encodeURI('AddContent.jsp?title='+title+'&aticle='+content)));
}
}
</script>
主函数是addDiv(),中间有个无用的获取url中username参数的方法,那个也无关紧要了。
解决方案 »
- 怎样把onmouseover的东西直接显示出来啊?在线等,马上结帖。
- jquery 如何 让text文本条中的内容被选中状态
- 为什么我这段代码不会运行
- 请教:全选 和 反选 checkbox
- javascript附值问题, 求高手帮帮忙!
- div的background-color设置为transparent后,就变成中空的了吗
- 模板文件中的导航点击后变色怎么做?
- 谁能帮我把这些vbs函数翻译成jscript的方法或函数?
- 我刚刚做了两个网站,几经修改,终于与大家见面了!多多请教(散分)
- 请问JavaScript和JSP的区别!
- spring boot 页面跳转问题
- 后面的olast 运行时没有数据 不知道为啥 请教大神
dispatcher 并不能把参数传过去。
你只能
request.setAttribute("username", username).forward(request, response);
request.getRequestDispatcher("/xiaochuangchangtan/windowrest.jsp").forward(request, response);function addDiv(){
var username="<%=request.getAttribute("username")%>";
//创建div
var articlediv = document.createElement('div');
//获取值
var title=document.getElementById("title").value;
var content=document.getElementById("aticle").value;
if(title=="" || content==""){
alert("请输入标题或内容");
}else{
//发表到后台
createRequest(encodeURI(encodeURI('AddContent.jsp?title='+title+'&aticle='+content)));
}
}
这里写错了, setAttribute后不跟 forward
参考下这个:https://blog.csdn.net/u012877472/article/details/50804568
楼上,君当如狼说对了,首先你参数要传过去。
==>
'<%=request.getContextPath()%>/xiaochuangchangtan/AddContent.jsp?title=........'