当前位置:编程学习 > JAVA >>

myeclipse做网站登陆功能有问题

我用myeclipse做一个网站,但是登陆功能始终无法实现,数据库用mysql,连接是连接成功了,但是是乱码,网页显示如下:

此外我的LoginServlet代码是:
package com.house.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.house.bean.Users;
import com.house.operation.UserOperation;

public class LoginServlet extends HttpServlet {

/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

 HttpSession session=request.getSession();
 //获取用户名和密码
 String uname=request.getParameter("uname");
 String upass=request.getParameter("upass");
 UserOperation userOperation=new UserOperation();
 //调用UserOperation类的Login方法进行登录验证
 Users user=userOperation.Login(uname, upass);
 if(user!=null)
 {
 session.setAttribute("user",user);
 }
 response.sendRedirect("/house/ShowList");
 
}

/**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
}

}


请各位帮忙,小妹感激不尽,看了一天了头都疼了,希望各位帮忙,谢谢~~~ MyEclipse 数据库 Java --------------------编程问答-------------------- 将response.sendRedirect("/house/ShowList");改用request.getRequestDispatcher("/house/ShoWList").forword(request,response);
你用重定向方法 一定向 参数就失效了,所以出现错误 --------------------编程问答-------------------- 按照你的方法改了,但是还是不行,错误是同样的。不过还是非常感谢你丫~ 希望继续有更改方案砸向我!! --------------------编程问答--------------------  你把你的JSP 页面代码贴出来看看 --------------------编程问答-------------------- web.xml配置servlet路径有问题 --------------------编程问答-------------------- 除 --------------------编程问答--------------------
引用 3 楼 mty583210969 的回复:
 你把你的JSP 页面代码贴出来看看


<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>杭州地铁楼盘交易网站</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">
-->

</head>

<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0
MARGINHEIGHT=0 >

<table width="930" border="1" align="center" cellspacing="0" height="512">
<tr>
<td colspan="5" valign="top" align="center" rowspan="1">
<p>
<img height="161" width="930"  src="images/logonew.png">
</p>
</td>
</tr>
<tr>
<td colspan="5" rowspan="1">
<table width="930"  cellspacing="0" align="center" border="0" bordercolor="#302C38" height="12">
<tbody>
<tr bgcolor="#302C38">
<td align="left">
<font face="Verdana, Arial, Helvetica, sans-serif"
color="#FFFFFF" size="2"> <b> <script
language="javascript" type="text/javascript">
day = new Date()
hr = day.getHours()
if ((hr == 23) || (hr == 0) || (hr == 1) || (hr == 2) || (hr == 3)
|| (hr == 4) || (hr == 5) || (hr == 6))
document.write("Good Night! this is ")
if ((hr == 7) || (hr == 8) || (hr == 9) || (hr == 10) || (hr == 11))
document.write("Good Morning! this is ")
if ((hr == 12) || (hr == 13) || (hr == 14) || (hr == 15) || (hr == 16))
document.write("Good Afternoon! this is ")
if ((hr == 17) || (hr == 18) || (hr == 19) || (hr == 20) || (hr == 21)
|| (hr == 22))
document.write("Good Evening! this is ")
var months = new Array(13);
months[1] = "January";
months[2] = "February";
months[3] = "March";
months[4] = "April";
months[5] = "May";
months[6] = "June";
months[7] = "July";
months[8] = "August";
months[9] = "September";
months[10] = "October";
months[11] = "November";
months[12] = "December";
var days = new Array(8);
days[1] = "Sunday";
days[2] = "Monday";
days[3] = "Tuesday";
days[4] = "Wednesday";
days[5] = "Thursday";
days[6] = "Friday";
days[7] = "Saturday";
var time = new Date();
var month2 = months[time.getMonth() + 1];
var date = time.getDate();
var year = time.getYear();
var day2 = days[time.getDay() + 1];
if (navigator.appName == "Netscape")
year = 1900 + year;
if (navigator.userAgent.indexOf("Opera") >= 0)
year = 1900 + year;
// You can make your own special days by just keeping to the same format below
if ((date == 24) && (month2 == "December"))
day2 = "Christmas eve";
if ((date == 25) && (month2 == "December"))
day2 = "Christmas day";
if ((date == 31) && (month2 == "December"))
day2 = "New years eve";
if ((date == 14) && (month2 == "February"))
day2 = "Valentine's day";
document.write(day2 + ", " + month2 + " ");
document.write(date + ", " + year);
//if ((date == 18) && (month2 == "May"))
// document.write("  Today, is John's Birthday");
if ((date == 29) && (month2 == "February"))
document.write("  Leap-year");
// -->
</script> </b> </font>
<br>
</td>
<form>
<!-- keep the form tag outside of the table data cell or it will make it larger -->
<td align="right" valign="middle">
<!-- If you want color in the navbar, you can add this style="background-color: #302C38; color: #81b2da; font-weight: bold; font-size: 10pt" But this will cause some problems in Netscape 4.7... Netscape 6 and Opera 6 will have no problems with this -->
<select name="quicknav" size="1"
onChange='top.location.href=this.form.quicknav.options[this.form.quicknav.selectedIndex].value'>
<option selected>
Quick Jump
</option>
<!-- follow the examples below to modify or add links to the QuickJump -->
<option value="http://www.genuitec.com">
Genuitec LLC
</option>
<option value="http://www.myeclipseide.com">
MyEclipse
</option>
</select>
</td>
<!-- keep the FORM tag outside of the table data cell or it will make it larger -->
</form>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>

<tr>
   
    <td width="172" valign="top"><%@ include file="myin.jsp"%></td>
    
    <td width="495">
<table border="6">
<tr>
<td width="3%"> </td>
<td width="97%">
<script>
function fswitch(id){
var o = document.getElementById(id);
if (o){
if (o.style.display == "none"){ o.style.display = "block"; } else {o.style.display = "none";}
}
}
</script>

<form id="form1" action="/house/SearchServlet" method="post">
 
 <div style="text-align:left;width:88%;height=30px">
<input name="title" type="text" >
<input type="submit" value="查询">
<%
   List list=(List)request.getAttribute("houseList");
   if(list!=null)
   {
   
 %>
共找到<%=list.size() %> 条记录!
<%
}
 %>
</div>
<table id="advSearch" border="4" bgColor="#ff0000">
<tr height="30px">
<td>车站:</td>
<td>
<select name="station">
<c:forEach  var="station" items="${requestScope.stationList}" >
   <option  value="${station.id }">${station.name }</option>
 </c:forEach>
</select>
</td>
</tr>
 
<tr height="30px">
<td>户型:</td>
<td><select name="room" >
<option value="0">不限--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>室  <select name="hall" style="width:60">
<option value="0">不限--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select> 厅</td>
</tr>
<tr>
<td>房屋类型:</td>
<td><select name="houseType" style="width:80">  
 <c:forEach var="type" items="${requestScope.typeList}">
  <option  value="${type.id }">${type.typeName}</option>
 </c:forEach>
</select></td>
</tr>
<tr>
<td>交易类型:</td>
<td><select name="poststate" style="width:60" >
    <option value="0">不限--</option>
<option value="1">出租</option>
<option value="2">求租</option>
<option value="3">合租</option>
<option value="4">转让</option>
</select> </td>
</tr>
</table> <br>
</form>
<table cellspacing="0" border="7" bgColor="#8000ff">
<tr>
<td colspan='3'><hr/></td>
</tr>
<tr>
<TD width='250' class="table_title">标题</TD>
<TD width='90' align='center' class="table_title">月租金</TD>
<TD align='center' class="table_title">发布日期</TD>
</tr>

  <c:forEach var="houseinfo" items="${requestScope.houseList}">
<tr>
<td>   
<a href='/house/ShowDetailServlet?houseId=${houseinfo.houseId}'>${houseinfo.title}</a>
 <c:if test="${sessionScope.user.uid==houseinfo.userId}">
[<a href="/house/ShowHouseServlet?id=${houseinfo.houseId }">编辑</a>] [<a href="/house/deleteservlet?id=${houseinfo.houseId }">删除</a>]
</c:if>
    
</td>
<td align='center' style='height:30px;'>
${houseinfo.rent }元
</td>
<td align='center'>
${houseinfo.postDate }
</td>
</tr>
</c:forEach>
<tr>
<td colspan='3'><hr/></td>
</tr>
</table>

</td>
</tr>
<tr>
<td colspan="2"><br></td>
</tr>
</table>
</td>
    
 </tr>



</tr>
<tr>
<td colspan="5"  >
<div align="center" >
</div>
<table width="100%"  cellspacing="0" cellpadding="0"
bgcolor="#ff8000" align="right" border="3">
<tr>
<td align="center" valign="bottom">
<p>
<a href="#"><font size="2"
face="Arial, Helvetica,sans-serif">About</font> </a><font
size="2" face="Arial, Helvetica, sans-serif"> | <a href="#">Contact Us</a> </font>
</p>
<p>
<font size="-1">Copyright (c) 2007 MySite -
http://www.mysite.com</font>
</p>
</td>
</tr>
</table>
<br>
</td>
</tr>
</table>

<br />

</BODY>
</html>
--------------------编程问答-------------------- 除 --------------------编程问答-------------------- Servlet里面下个断点调试下,是不是你路径有问题,或者页面jsp不能写在web-inf下面 --------------------编程问答--------------------  if(user!=null)
         {
             session.setAttribute("user",user);
            response.sendRedirect("/house/ShowList");
         }else{
             // 给个错误提示,看看是否进来了
             }
        --------------------编程问答--------------------
<%@ page language="java" contentType="text/html; charset=GBK"

pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script language="javascript">

function login(){
if( document.myForm.uname.value ==""){
alert("用户名不能为空");
return false;
}else if(document.myForm.upass.value == ""){
alert("密码不能为空");
return false;
} else {
return true;
}
}

</script>
<c:if test="${sessionScope.user!=null}">

<table align="center" width="378" height="148">
<tr>
<td width='' colspan="5"><font color='red'>当前用户:${sessionScope.user.uname }

</font>
</td>
</tr>

<tr>
<td width='100'><br><br><a href="/house/PostServlet">发布租房信息</a></td>
</tr>


<tr>
<td width='100'><a href="/house/ShowList.jsp">返回首页</a></td>
</tr>
<tr>
<td width='100'><a href="edit_pwd.jsp">[修改密码]</a></td>
</tr>
<tr>
<td width='100'><a href="/house/LoginOutServlet">[注销]</a></td>
</tr>
</table>
</c:if>
<c:if test="${sessionScope.user==null}">
<form name="f1" action="/house/loginservlet" method="post" onsubmit="return login();">
   <p> </p>
  <table align="center" width="190" height="190">
<tr>
<td colspan="2" align="center">用户名:</td>
</tr>
<tr>
  <td colspan="2" align="center"><input type="text" name="uname" size=10" class="editbox"></td>
</tr>
<tr>
<td colspan="2" align="center">密  码:</td>
</tr>
<tr>
  <td colspan="2"align="center"><input type="password" name="upass" size=10"  class='editbox'></td>
  </tr>
<tr>
<td align="center">
<input type="hidden" name="sign" value="login"/>
<input type="submit" value="登陆" class='btn'>
<input type="reset" value="重置" class='btn'>
</td>
</tr>
<tr align="center">
  <td><a href="register.jsp">注册用户</a></td>
</tr>
<tr align="center">
<td width='100'><a href="/house/ShowList.jsp">返回首页</a></td>
</tr>
  </table>
   </form>
</c:if>
--------------------编程问答-------------------- response.sendRedirect("/house/ShowList");
aciton配的有问题吧 --------------------编程问答--------------------
引用 11 楼 fengspg 的回复:
response.sendRedirect("/house/ShowList");
aciton配的有问题吧

什么问题? --------------------编程问答--------------------
引用 12 楼 icecreamfruit 的回复:
Quote: 引用 11 楼 fengspg 的回复:

response.sendRedirect("/house/ShowList");
aciton配的有问题吧

什么问题?
你可以在浏览器直接访问你的servlet,看可以吗 --------------------编程问答-------------------- 个人觉得 应该是你要跳转的JSP 不在你对应的webroot 下面  你看看你要跳转的jsp 是不是指定的路径 可以直接在把路径打在浏览器访问的路径上看能不能跳转到你想要去的jsp页面上 --------------------编程问答-------------------- 同意楼上,检查一下路径是否正确 --------------------编程问答-------------------- 把你demo拿来,我看看, --------------------编程问答-------------------- 你登录 表单提交action应该转到/house/ShowList,怎么还是house/loginservlet? --------------------编程问答-------------------- 弱弱问下楼主是JSP跳到LoginServlet还是LoginServlet跳到/house/ShowList有问题。
LZ404错误就是路径有问题!建议
1.检查一跳转地方的路径,相对路径改为绝对路径试试
2.你用是eclipse是什么版本的如果是最新版的4.2就不会存在web.xm配置问题。所以这个可以放心!
3.在网页的地址栏里面直接输入你要跳转的servlet的地址试试看!(记得带上参数哦,看看能不能跳出来),或者直接运行servlet试试看!看看是否会报错!如果不报错的话,就把运行后地址栏出现的地址直接复制到你前面sendRedirect参数里面,或者是form action=“”里面!
5.sendRedirect后面参数试试不加路径就是直接写文件名字!如果是jsp就写**.jsp,HTML就写**.html或者是servlet就直接写文件名字就行了!后面不用带后缀的!
--------------------编程问答-------------------- 谢谢各位,果然是路径有问题,是我.jsp里面的路径与web.xml里的路径没有对上,非常感谢各位! --------------------编程问答-------------------- 果然是xml的问题 --------------------编程问答-------------------- 讲那么多  有P用  一句话 自己看看 转发 与 重定向 的区别  再看看你的代码自己想想就会明白的 还有 404不是 乱码是 你要访问的 网页找不到 --------------------编程问答-------------------- 都用同一个编码就不会乱码,另外404错误是页面没找到,是你的servlet或者页面跳转路径没写对,请查看一下web.xml文件中servlet的参数配置, --------------------编程问答-------------------- 404是找不到页面,应该是路径写的有问题,现在浏览器上访问页面看看页面是否能找到,然后对应路径检查是否正确  --------------------编程问答-------------------- 这。。。
一般用myeclipse自动生成servlet时你确定好了那个url。
然后在用到的时候才不会出错。小妹记住了。 --------------------编程问答-------------------- action里面的地址在寻找loginservlet时,路径设置很可能不正确,你的图片显示这个类找不到,应该是路径和问题
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,