JSP开发问题

zhangfengyi 2015-11-03 09:10:01
第一次开发JSP程序,按照网上介绍的教程做的表单提交程序,点提交没有反应,不知道是哪里的错,怎样调试,把页面贴出来,帮忙分析一下。
JSP页面:

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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<format action="TestServlet">
w3c在线教程:
<input type="text" name="weburl" size="100"/><br/>
<input type="submit" value="提交"/>
</format>
</body>
</html>

TestServlet代码:

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;


public class TestServlet extends HttpServlet {

private static final long serialVersionUID=-1575386983723846021L;
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String weburl=request.getParameter("weburl");
System.out.print(weburl);
if(weburl!=null && weburl.equals("http://www.w3csschool.com"))
{
request.setAttribute("weburl", "你输入的是W3C在线教程网");
}
else
{
request.setAttribute("weburl", "你输入的网址在数据库中未有记录");
}
RequestDispatcher dispatcher=request.getRequestDispatcher("result.jsp");
dispatcher.forward(request, response);
}

}


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>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/servlet/TestServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


result.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>结果是:</title>
</head>

<body>
<%=request.getAttribute("weburl")%>
</body>
</html>
...全文
288 点赞 收藏 15
写回复
15 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zhangfengyi 2015-11-05
这样就可以了 String name=new String(request.getParameter("Name").getBytes("ISO-8859-1"),"UTF-8");
回复
zhangfengyi 2015-11-05
新问题,jsp 页面与servlet页面都设置了utf-8编码,为什么中文字符还是乱码 jsp页面:

<%@page pageEncoding="utf-8" contentType="text/html;charset=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">d
<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">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
servlet页面:

response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
回复
zhangfengyi 2015-11-03
TestServlet.java如下,没有返回结果,我怎样调试

private static final long serialVersionUID=-1575386983723846021L;
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//String merchID=request.getParameter("MerchID");
		
		//if(merchID!=null)
		//{
		//	request.setAttribute("merchID", "你输入的是W3C在线教程网");
		//}
		//else
		//{
		//	request.setAttribute("merchID", "你输入的网址在数据库中未有记录");
		//}
		//RequestDispatcher dispatcher=request.getRequestDispatcher("result.jsp");
		//dispatcher.forward(request, response);
		
		try
		{
			//String merchid=request.getParameter("MerchID");
			//String msgid=request.getParameter("MsgID");
			//String cardno=request.getParameter("CardID");
			//System.out.println(merchid);
			//RequestDispatcher dispatcher=request.getRequestDispatcher("result.jsp");
			//dispatcher.forward(request, response);
			String cardno="6228480030976274";
			String certino="410711199002251";
			String name="王*峰";
			String phone="13918315*";
			
			String limitdate="";
			String cvn2="";
			
			String msgid=getCurrentDateTime();
			String merchid="33350018";
			String certitype="01";
			String cerPwd="1234567890";
			String deskey="6350105e40013e7de0530b14640a74e2";
			
			String url="http://101.254.166.186/certification/check";
			int conntimeout=50000;
			int sotimeout=60000;
			
			CertiRequest req=new CertiRequest();
			req.setMerchid(merchid);
			req.setMsgid(msgid);
			req.setCardno(cardno);
			req.setCertitype(certitype);
			req.setCertino(certino);
			req.setName(name);
			req.setPhone(phone);
			req.setLimitdate(limitdate);
			req.setCvn2(cvn2);
			req.setDeskey(deskey);
			req.setConntimeout(conntimeout);
			req.setSotimeout(sotimeout);
			req.setUrl(url);
			
			RequestService serv=new RequestService();
			CertiResponse resp;
			try
			{
				resp=serv.send(req);
			}
			catch(Exception e)
			{
				e.printStackTrace();
				return;
			}
			System.out.println("返回数据:"+resp.toString());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static String getCurrentDateTime()
	{
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyddHHmmss");
		return String.valueOf(dateFormat.format(new Date()));
	}
	
	public static String productFigure()
	{
		Random rnd=new Random();
		int num=rnd.nextInt();
		if(num<=0 || num>999999)
		{
			return productFigure();
		}
		return String.valueOf(num);
	}
回复
zhangfengyi 2015-11-03
问题是解决了,还有其他问题
回复
qq_17553329 2015-11-03
好像解决了
回复
I_am_a_Beginner 2015-11-03
进来学习!
回复
Usher丶AckMan 2015-11-03
右击项目属性,不是有个java build paths,里面加就可以了
回复
zhangfengyi 2015-11-03
在TestServlet.java中怎样引用jar包

2015/09/23  19:04           263,965 commons-codec-1.9.jar
2015/09/23  18:19           305,001 commons-httpclient-3.1.jar
2015/09/23  19:03            62,050 commons-logging-1.1.3.jar
2015/01/13  10:14           313,898 dom4j-1.6.1.jar
2015/01/11  13:45           489,884 log4j-1.2.17.jar
2015/10/10  11:33            23,573 payCheck-1.0.1.jar
回复
zhangfengyi 2015-11-03
是路径的问题,改了下路径就可以了,配置文件中的路径<url-pattern>/TestServlet</url-pattern>,改成这样的就可以了
回复
你把action的值改一下,改成这个/servlet/TestServlet
回复
sinat_31535993 2015-11-03
引用 2 楼 zhangfengyi 的回复:
[quote=引用 1 楼 sinat_31535993 的回复:] <format action="TestServlet"> w3c在线教程: <input type="text" name="weburl" size="100"/><br/> <input type="submit" value="提交"/> </format> 第一次见form表单写成format的
写错了,问题又来了,这个路径如何写[/quote] <url-pattern>/servlet/TestServlet</url-pattern>这不就是你自己设置的路径!
回复
zhangfengyi 2015-11-03
引用 1 楼 sinat_31535993 的回复:
<format action="TestServlet">
w3c在线教程:
<input type="text" name="weburl" size="100"/><br/>
<input type="submit" value="提交"/>
</format>
第一次见form表单写成format的

写错了,问题又来了,这个路径如何写
回复
sinat_31535993 2015-11-03
<format action="TestServlet"> w3c在线教程: <input type="text" name="weburl" size="100"/><br/> <input type="submit" value="提交"/> </format> 第一次见form表单写成format的
回复
心随自在飞 2015-11-03
设段点,debug模式! 一步一步慢慢走!
回复
朗晴 2015-11-03
多看多练 加油。
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-11-03 09:10
社区公告
暂无公告