正如题:是Javaservlet还是asp.net,学过java的人都用应该知道Javaservlet,但学过asp.net的人不见得知道 ashx,ashx是一般处理程序,也是一种比较老的技术了,ashx处理能力和Javaservlet很相似,有代码为证:
javaervlet页面代码:
- package com.xz.form;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class MyInfoForm extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=utf-8");
- PrintWriter out = response.getWriter();
- //response.setCharacterEncoding("utf-8");
- out.println("<form action='/ServletDemo3/ServletFormCL' method='post'>");
- out.println("用户名:<input type='text' name='username'/><br/>");
- out.println("密 码:<input type='password' name='password'/><br/>");
- out.println("性 别:<input type='radio' name='sex' value='男'>男<input type='radio' name='sex' value='女'>女<br/>");
- out.println("你的爱好:<input type='checkbox' name='aihao' value='音乐'>音乐<input type='checkbox' name='aihao' value='旅游'>旅游<br/>");
- out.println("你所在的城市:<select name='city'><option value='湖南'>湖南</option><option value='北京'>北京</option></select><br/>");
- out.println("自我简介:<br/><textarea clos='20' rows='10' name='intro'></textarea><br/>");
- out.println("上传照片:<input type='file' name='file'/><br/>");
- out.println("<input type='submit' value='提交'>");
- out.println("</form>");
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=utf-8");
- PrintWriter out = response.getWriter();
- this.doGet(request, response);
- }
- }
javaservlet处理页面代码
- package com.xz.form;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class ServletFormCL extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");
- response.setContentType("text/html;charset=utf-8");
- PrintWriter out = response.getWriter();
- String name = request.getParameter("username");
- String pwd = request.getParameter("password");
- String sex = request.getParameter("sex");
- String[] aihao = request.getParameterValues("aihao");
- String city = request.getParameter("city");
- String intro = request.getParameter("intro");
- out.print("用户名是:"+name);
- out.println("<br/>密码是:"+pwd);
- out.println("<br/>性别:"+sex);
- out.println("</br>爱好:");
- if(aihao!=null)
- {
- for(int i=0 ; i<aihao.length; i++)
- {
- out.println(" "+aihao[i]+" ");
- }
- }
- else
- {
- out.println("您没有选择爱好!");
- }
- out.println("<br/>自我简介:"+intro);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=utf-8");
- PrintWriter out = response.getWriter();
- this.doGet(request, response);
- }
- }
下面我再看看ashx代码:
- <%@ WebHandler Language="C#" Class="asp" %>
- using System;
- using System.Web;
- public class asp : IHttpHandler {
- public void ProcessRequest (HttpContext context) {
- context.Response.ContentType = "text/html";
- String username = context.Request["username"]; //得到from表单username的值
- String pwd = context.Request["password"]; //得到from表单password的值
- context.Response.Write("用户名:"+username+"密码:"+pwd); //打印到屏幕上
- context.Response.Write("<br/>");
- context.Response.Write("<form action='asp.netTest1.ashx' method='post'>");
- context.Response.Write("<input type='hidden' name='ispostback' value='true'/>");
- context.Response.Write("用户名:<input type='text' name='username' /><br/>");
- context.Response.Write("密 码:<input type='password' name='password' /><br/>");
- context.Response.Write("<input type='submit' value='提交'/>");
- context.Response.Write("</form>");
- String ispostback = context.Request["ispostback"]; //隐藏字段所传的值
- if (ispostback=="true")
- {
- context.Response.Write("提交进入.........");
- }
- else
- {
- context.Response.Write("直接进入.........");
- }
- }
- public bool IsReusable {
- get {
- return false;
- }
- }
- }
-----不的不说微软确实很强大,不光技术易用,并且还有深意,虽然有模仿的嫌疑,但不得不说技术都是想通,谁提供的解决方案好,谁就是老大,但毫无疑问微软的解决方案确实不错,为程序员做了很多简化动作,只要了解内部原理,其他的都不在话下了!