servlet页面跳转失败,出现空白的servlet页面,这是为什么??
import javax.servlet.http.HttpServlet;import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javabean.user;
import DB.DB;
public class MainServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setContentType("gb2312");
String username = request.getParameter("LoginBoard_LoginName");
String pwd = request.getParameter("LoginBoard_LoginPassword");
DB db = new DB();
HttpSession session = request.getSession();
user user1 = (user) session.getAttribute("user");
if (user1 == null) {
user1 = db.selectUser(username, pwd);
}
session.setAttribute("user1", user1);
if (user1 != null) {
ArrayList a1 = db.findLyInfo();
session.setAttribute("a1", a1);
response.sendRedirect("MessageBoard_Main.jsp");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
--------------------编程问答-------------------- 这个是补充,我怕是数据库处理的时候出了问题,现在把数据库连接的代码页贴出来
package DB;
import java.sql.*;
import javabean.message;
import javabean.user;
import java.util.ArrayList;
public class DB {
Connection ct;
PreparedStatement pstmt;
public DB() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager
.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=MessageBoard",
"sa", "123");
} catch (Exception e)
{
e.printStackTrace();
}
}
public user selectUser(String username, String password) {
try {
pstmt = ct
.prepareStatement("select*from[user]where username=? and password=?");
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
user user1 = new user();
while (rs.next()) {
user1.setId(rs.getInt(1));
user1.setUsername(rs.getString(2));
user1.setPassword(rs.getString(3));
return user1;
}
return user1;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public ArrayList findLyInfo(){
try{
ArrayList a1=new ArrayList();
pstmt=ct.prepareStatement("select * from [MessageBoard]");
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
message mb=new message();
mb.setId(rs.getInt(1));
mb.setUserid(rs.getString(2));
mb.setDate(rs.getDate(3));
mb.setTitle(rs.getString(4));
mb.setContent(rs.getString(5));
a1.add(mb);
}
return a1;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public String getusername(int id){
String username=null;
try{
pstmt=ct.prepareStatement("select username from[user] where id=?");
pstmt.setInt(1,id);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
return username;
}}catch(Exception e){
e.printStackTrace();
return null;
}
return null;}
} --------------------编程问答-------------------- 楼主,你提交的是form表单吧,设置method="post"了吧,其实你直接把doGet方法里的程序写到doPost里面,你用的是post提交,doGet方法不需要,一个doPost方法就行了 --------------------编程问答-------------------- 建议在post方法里写上doGet(request,response),以后就不用管什么方式提交表单了,在一个方法里面处理就行了
--------------------编程问答-------------------- 建议把xml配置文件和那个空白页面的地址发出来。 --------------------编程问答--------------------
XML配置:
?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>myServlet.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/MainServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>MessageBoard_Login.jsp</welcome-file>
</welcome-file-list>
</web-app>
空白页面地址:
http://20120214-1351:8080/JavaEE_MessageBoard/MainServlet
(下面的页面是空白的)
这个贴白不能贴图么?本来想截图,方便点。 --------------------编程问答-------------------- 还是没人告诉我,空白页面怎么办啊?什么post方法,get方法那不是重点吧?? --------------------编程问答-------------------- 可能是你的Servlet配置文件出现问题了吧? --------------------编程问答--------------------
我已经贴出了配置文件,我不知道问题在哪? --------------------编程问答-------------------- 你看看浏览器源代码里面有内容吗? --------------------编程问答--------------------
没有,也是空白的 --------------------编程问答-------------------- 按你的逻辑应该是没进到这里
if (user1 != null) {
ArrayList a1 = db.findLyInfo();
session.setAttribute("a1", a1);
response.sendRedirect("MessageBoard_Main.jsp");
}
也就是user1 == null 了
自己debug看看 --------------------编程问答-------------------- doget dopost没关系 不管什么提交方法你已经在dopost中加入doGet(request, response)
request.setCharacterEncoding("gb2312");//这个最好改成utf-8 要和jsp编码一样
response.setContentType("gb2312");
String username = request.getParameter("LoginBoard_LoginName");//首先你一打开页面 请问这个值你从什么地方赋值的。
String pwd = request.getParameter("LoginBoard_LoginPassword");
DB db = new DB();
HttpSession session = request.getSession();//创建session
user user1 = (user) session.getAttribute("user");//请问这个值你从什么地方赋值的。首先你一打开页面
if (user1 == null) {
user1 = db.selectUser(username, pwd);//查询 没有username pwd你查什么 下面就不说了
}
session.setAttribute("user1", user1);
if (user1 != null) {
ArrayList a1 = db.findLyInfo();
session.setAttribute("a1", a1);
response.sendRedirect("MessageBoard_Main.jsp");
}
你在jsp怎么遍历的 你不会没遍历吧 --------------------编程问答-------------------- 空白一般都是没查到数据 亲 --------------------编程问答--------------------
public user selectUser(String username, String password) {
try {
pstmt = ct
.prepareStatement("select*from[user]where username=? and password=?");
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
user user1 = new user();
while (rs.next()) {
user1.setId(rs.getInt(1));
user1.setUsername(rs.getString(2));
user1.setPassword(rs.getString(3));
return user1;
}
return user1;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
pstmt = ct
.prepareStatement("select*from[user]where username=? and password=?");
空格 select * from 如果运行好这里应该报错的 没保持就是应该没运行到这里 现在搞.net
你应该说的详细点 怎么赋值 实现什么功能这样我们才能帮忙 --------------------编程问答--------------------
支持二楼
直接重写service方法就ok --------------------编程问答--------------------
支持二楼
直接重写service方法就ok
我表示和method属性无关,我也遇到了这样的问题,现在把页面代码贴出来
<table class="table">
<tr class="table_header">
<td>
ID
</td>
<td>
Username
</td>
<td>
Gendar
</td>
<td>
Age
</td>
<td>
</td>
</tr>
<%
UserDao ud=new UserDaoImpl();
ArrayList<User> list=ud.selectAll();
if(list.size()>0){
for(User u:list){
%>
<tr class="row1">
<td>
<%=u.getId() %>
</td>
<td>
<%=u.getUsername() %>
</td>
<td>
<%
int i=u.getGender();
if(i>0){
%>
<%='男' %>
<%
}else{
%>
<%='女' %>
<%}
%>
</td>
<td>
<%=u.getAge() %>
</td>
<td>
<a href="delete.do?id=<%=u.getId() %>" onclick="return confirm('你确定删除编号为<%=u.getId() %>的用户吗?')">删除</a>
</td>
</tr>
<%
}
}
%>
</table>
诚求各位大神指教一二。 --------------------编程问答-------------------- HttpSession session = request.getSession();
user user1 = (user) session.getAttribute("user");
if (user1 == null) {
user1 = db.selectUser(username, pwd);
}
假设你是第一次访问你的servlet那么session是没有值的,你可以在if里面加上response.getwriter.print("首次访问本站");你看看是不是出现数据了,这次访问后,session你就在下面存值了,你就获取的到session中的数据显示了
补充:Java , Java EE