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

困扰许久的jsp表单提交路径问题,请各位大神们帮忙解决啊

前台login.jsp:
<form action="/servlet/NewCheck" method="post">
       <table align="center">
        <tr>
         <td class="logintd1">用户名:</td>
         <td><input type="text" name="username"id="username" size="18"onblur="checkname();"/><label class="red" id="divusername"></label> </td>
        </tr>
        <tr>
         <td>密码:</td>
         <td> <input type="password" id="password1" size="18" onblur="checkpwd();"/> <label class="red" id="divpassword1"></label></td>
        </tr>
        <tr>
         <td>        <input type="submit" value="登录"/></td>
         <td><a href="#">忘记密码?</a></td>
        </tr>
        <tr>
         <td colspan="2"><a href="index.jsp">返回主页</a> | <a href="register.jsp">快速注册</a></td>
        </tr>
       </table>
     </form>
servlet代码:
NewCheck.java
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class NewCheck extends HttpServlet {


public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String username=request.getParameter("username");
HttpSession session=request.getSession();
String temp=session.getAttribute("logined").toString();
if(temp.equals("username"))
{
session.setAttribute("logined", "username");
RequestDispatcher view=request.getRequestDispatcher("index.jsp");
    view.forward(request,response);
}
else
{
session.setAttribute("logined", "username");
RequestDispatcher view=request.getRequestDispatcher("login.jsp");
    view.forward(request,response);
}

out.flush();
out.close();
}
       
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
        doPost(request,response);

}

public void init() throws ServletException {
// Put your code here
}

}
web.xml配置:
<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>NewCheck</servlet-name>
  <servlet-class>NewCheck</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>NewCheck</servlet-name>
  <url-pattern>/servlet/NewCheck</url-pattern>
 </servlet-mapping>

tomcat 6.0运行结果提示:
type Status report

message /servlet/NewCheck

description The requested resource (/servlet/NewCheck) is not available.

希望大家帮帮忙啊 JSP jsp表单提交,404错误 Servlet --------------------编程问答-------------------- 项目路径:
--------------------编程问答--------------------
引用 1 楼 he_shan_ 的回复:
项目路径:



这样action="MyBBS/servlet/NewCheck"  
或者在jsp页面上加红色部分
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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">
<script type="text/javascript" src="js/function.js"></script>
  </head>


先这样试试,看看行不行 --------------------编程问答-------------------- jsp页面加这个
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%>
<base href="<%=basePath%>"> --------------------编程问答--------------------
引用 2 楼 q35335010 的回复:
Quote: 引用 1 楼 he_shan_ 的回复:

项目路径:



这样action="MyBBS/servlet/NewCheck"  
或者在jsp页面上加红色部分
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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">
<script type="text/javascript" src="js/function.js"></script>
  </head>


先这样试试,看看行不行
还是The requested resource (/MyBBS/servlet/NewCheck) is not available.
--------------------编程问答-------------------- <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>NewCheck</servlet-name>
  <servlet-class>MyBBS.NewCheck</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>NewCheck</servlet-name>
  <url-pattern>/NewCheck</url-pattern>
 </servlet-mapping> --------------------编程问答-------------------- web.xml里面servlet的路径不对 --------------------编程问答-------------------- 现在我把web.xml改为:
<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>NewCheck</servlet-name>
  <servlet-class>NewCheck</servlet-class>
 </servlet>
<servlet-mapping>
  <servlet-name>NewCheck</servlet-name>
  <url-pattern>/NewCheck</url-pattern>
 </servlet-mapping>
表单提交改为了:
<form action="NewCheck" method="get">

之后在myeclipse 8.5里运行能成功,可是关了myeclipse的内部服务器而去运行外部Tomcat/6.0.16时,出现404错误:还是提示The requested resource (/MyBBS/NewCheck) is not available。
是我的tomcat配置错了吗?各位大神啊,快出来吧······ --------------------编程问答--------------------
引用 7 楼 he_shan_ 的回复:
现在我把web.xml改为:
<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>NewCheck</servlet-name>
  <servlet-class>NewCheck</servlet-class>
 </servlet>
<servlet-mapping>
  <servlet-name>NewCheck</servlet-name>
  <url-pattern>/NewCheck</url-pattern>
 </servlet-mapping>
表单提交改为了:
<form action="NewCheck" method="get">

之后在myeclipse 8.5里运行能成功,可是关了myeclipse的内部服务器而去运行外部Tomcat/6.0.16时,出现404错误:还是提示The requested resource (/MyBBS/NewCheck) is not available。
是我的tomcat配置错了吗?各位大神啊,快出来吧······



404看看tomcat6.0运行的时候是不是报错了
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,