index.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function search(){
with(document.biao){
action='../servlet/SelectServlet';
method='post';
target='_blank';
submit();
}
}
</script>
</head>
<body>
<form name='biao'>
<input type='button' name='btn' value='查询'onClick='search()'/>
</form>
</body>
</html>SelectServlet文件:
package com.feifei.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;public class SelectServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
Context ctx = null;
DataSource ds = null;
Connection conn = null;
Statement st = null;
PreparedStatement prst = null;
ResultSet rs = null;
ResultSetMetaData meta = null;
String sql = "SELECT * FROM feifei";
try {
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/feifei");
conn = ds.getConnection();
st = conn.createStatement();
rs = st.executeQuery(sql);
out.print("<table border='1' bgcolor='black'>");
while(rs.next()){
out.print("<tr>");
out.print("<td bgcolor='white'>");
out.print(rs.getString(1));
out.print("</td>");
out.print("<td bgcolor='white'>");
out.print(rs.getString(2));
out.print("</td>");
out.print("<td bgcolor='white'>");
out.print(rs.getInt(3));
out.print("</td>");
out.print("</tr>");
}
out.print("</table>");
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.flush();
out.close();
}}web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>SelectServlet</servlet-name>
<servlet-class>com.feifei.servlet.SelectServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>SelectServlet</servlet-name>
<url-pattern>/servlet/SelectServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<res-ref-name>jdbc/feifei</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>问题:在点击“查询”按钮的时候,在调用servlet时出现HTTP 400错误,而且地址栏是:http://localhost:8080/server/SelectServer,这是什么原因。。
(本人刚学J2EE,请大侠们多多指教)
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function search(){
with(document.biao){
action='../servlet/SelectServlet';
method='post';
target='_blank';
submit();
}
}
</script>
</head>
<body>
<form name='biao'>
<input type='button' name='btn' value='查询'onClick='search()'/>
</form>
</body>
</html>SelectServlet文件:
package com.feifei.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;public class SelectServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
Context ctx = null;
DataSource ds = null;
Connection conn = null;
Statement st = null;
PreparedStatement prst = null;
ResultSet rs = null;
ResultSetMetaData meta = null;
String sql = "SELECT * FROM feifei";
try {
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/feifei");
conn = ds.getConnection();
st = conn.createStatement();
rs = st.executeQuery(sql);
out.print("<table border='1' bgcolor='black'>");
while(rs.next()){
out.print("<tr>");
out.print("<td bgcolor='white'>");
out.print(rs.getString(1));
out.print("</td>");
out.print("<td bgcolor='white'>");
out.print(rs.getString(2));
out.print("</td>");
out.print("<td bgcolor='white'>");
out.print(rs.getInt(3));
out.print("</td>");
out.print("</tr>");
}
out.print("</table>");
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.flush();
out.close();
}}web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>SelectServlet</servlet-name>
<servlet-class>com.feifei.servlet.SelectServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>SelectServlet</servlet-name>
<url-pattern>/servlet/SelectServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<res-ref-name>jdbc/feifei</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>问题:在点击“查询”按钮的时候,在调用servlet时出现HTTP 400错误,而且地址栏是:http://localhost:8080/server/SelectServer,这是什么原因。。
(本人刚学J2EE,请大侠们多多指教)
解决方案 »
- 关于Hibernate中的动态代理是起什么作用的呢?
- 求大家看个问题??
- htmlparser爬取网页时报Connection reset异常
- 求教:如何选择服务器?
- CSDN上没有人了啊,失望……………………………………………………………………………………………………………………………………………
- 有本《精通struts》那书的内容能否用到现在的struts1.2?
- Struts 资源文件问题
- 急!急!急!急!急!急!急!急!急!急!急!急!急!!
- 请问怎么修改config.xml以把ejb的jar文件发布到weblogic上?
- Spring高并发下Cglib代理性能问题
- web service 接口参数问题
- session的疑惑
1.web.xml:<servlet-mapping>
<servlet-name>SelectServlet </servlet-name>
<url-pattern>/servlet/SelectServlet </url-pattern>
</servlet-mapping> 2.with(document.biao){
action='../servlet/SelectServlet'; 这个两个路径不一样。你去掉'../'试试,