Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 无法使用JSP获取url变量_Html_Jsp - Fatal编程技术网

Html 无法使用JSP获取url变量

Html 无法使用JSP获取url变量,html,jsp,Html,Jsp,我正在为学校编写一个在线文本编辑器。我已经能够用自定义名称保存文件,所以这不是问题所在。使用我拥有的HTML表单,它将TextArea的文本提交到用户指定的文件中,然后将url设置为blah.org/text.jsp?status=gen。在代码后面,如果变量status==gen,我让程序编写该文件,否则什么也不做。我无法在单击submit时创建文件,我认为这与我在URL中获取变量有关。这是我的密码: /* Set the "user" variable to the "user" a

我正在为学校编写一个在线文本编辑器。我已经能够用自定义名称保存文件,所以这不是问题所在。使用我拥有的HTML表单,它将TextArea的文本提交到用户指定的文件中,然后将url设置为blah.org/text.jsp?status=gen。在代码后面,如果变量status==gen,我让程序编写该文件,否则什么也不做。我无法在单击submit时创建文件,我认为这与我在URL中获取变量有关。这是我的密码:

    /* Set the "user" variable to the "user" attribute assigned to the session */
String user = (String)session.getAttribute("user");
String status = request.getParameter("status");
    /* Get the name of the file */
String name = request.getParameter("name");
    /* Set the path of the file */
String path = "C:/userFiles/" + user + "/" + name + ".txt";
/* Get the text in a TextArea */
String value = request.getParameter("textArea");
    /* If there isn't a session, tell the user to Register/Log In */
if (null == session.getAttribute("user")) {
out.println("Please Register/Log-In to continue!");
    if (status == "gen") {
        try {
            FileOutputStream fos = new FileOutputStream(path);
            PrintWriter pw = new PrintWriter(fos);
            pw.println(value);
            pw.close();
            fos.close();
    }
    catch (Exception e) {
        out.println("<p>Exception Caught!");
    }
} else {
out.println("Error");
}
}
/*将“user”变量设置为分配给会话的“user”属性*/
String user=(String)session.getAttribute(“用户”);
字符串状态=request.getParameter(“状态”);
/*获取文件名*/
字符串名称=request.getParameter(“名称”);
/*设置文件的路径*/
String path=“C:/userFiles/”+user+“/”+name+“.txt”;
/*获取文本区域中的文本*/
字符串值=request.getParameter(“textArea”);
/*如果没有会话,请告诉用户注册/登录*/
if(null==session.getAttribute(“用户”)){
out.println(“请注册/登录以继续!”);
如果(状态=“发电机”){
试一试{
FileOutputStream fos=新的FileOutputStream(路径);
PrintWriter pw=新的PrintWriter(fos);
pw.println(值);
关闭();
fos.close();
}
捕获(例外e){
out.println(“异常被捕获!”);
}
}否则{
out.println(“错误”);
}
}
及表格:

<form name="form1" method="POST" action="text.jsp?status=gen">
<textarea cols="50" rows="30" name="textArea"></textarea>
<center>Name: <input type="text" name="name" value="Don't put a .ext"> <input type="submit" value="Save" class="button"></center>
other code here </form>

姓名:
这里还有其他代码
更换

if (status == "gen") {

甚至更好

if ( "gen".equals(status) ) {

您的第一条语句不执行对象相等,而执行字符串相等。因此,它总是返回false。

您将字符串与
=
进行比较,而不是与
.equals()
进行比较<代码>=比较字符串是否为相同的对象实例,而不是它们是否包含相同的字符

一定要读这本书。流应该始终在finally块中关闭,或者您应该使用Java7try-with-resources构造

if ( "gen".equals(status) ) {